Итак, я разрабатываю приложение для чата и тестирую его на симуляторе android (вариант с пикселем 3). Но теперь я пытаюсь заставить его работать в другом эмуляторе (на этот раз я выбираю пиксель 2) и не запускается.
Я передам часть кода, включая MainActivity.
Это из LogCat
2020-01-28 01:47:27.799 10456-10456/com.example.textmefinal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.textmefinal, PID: 10456
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.textmefinal/com.example.textmefinal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.textmefinal.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-01-28 01: 47: 57.456 10456-10494 / ком. Например
}
Активность входа
package com.example.textmefinal;
publi c Класс LoginActivity расширяет AppCompatActivity {
private FirebaseAuth mAuth;
private ProgressDialog loadingBar;
private Button LoginButton, PhoneLoginButton;
private EditText UserEmail, UserPassword;
private TextView NeedNewAccountLink, ForgetPasswordLink;
private DatabaseReference UsersRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
InitializeFields();
NeedNewAccountLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToRegisterActivity();
}
});
LoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AllowUserToLogin();
}
});
}
@Override
protected void onStart()
{
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null)
{
SendUserToMainActivity();
}
}
private void AllowUserToLogin() {
String email = UserEmail.getText().toString();
String password = UserPassword.getText().toString();
if (TextUtils.isEmpty(email)){
Toast.makeText(this, "Please enter email...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this, "Please enter password...", Toast.LENGTH_SHORT).show();
}
else {
loadingBar.setTitle("Sign In");
loadingBar.setMessage("Please wait...");
loadingBar.setCanceledOnTouchOutside(true);
loadingBar.show();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
String currentUserId = mAuth.getCurrentUser().getUid();
String deviceToken = FirebaseInstanceId.getInstance().getToken();
UsersRef.child(currentUserId).child("device_token")
.setValue(deviceToken)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
SendUserToMainActivity();
Toast.makeText(LoginActivity.this, "Logged in Successful", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
else {
String message = task.getException().toString();
Toast.makeText(LoginActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
private void InitializeFields() {
LoginButton = (Button) findViewById(R.id.login_button);
PhoneLoginButton = (Button) findViewById(R.id.phone_login_button);
UserEmail = (EditText) findViewById(R.id.login_email);
UserPassword = (EditText) findViewById(R.id.login_password);
NeedNewAccountLink = (TextView) findViewById(R.id.need_new_account_link);
ForgetPasswordLink = (TextView) findViewById(R.id.forget_password_link);
loadingBar = new ProgressDialog(this);
}
private void SendUserToMainActivity() {
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
private void SendUserToRegisterActivity() {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(registerIntent);
}
}
Регистрация активности
package com.example.textmefinal;
publi c Класс RegisterActivity расширяет AppCompatActivity {
private Button CreateAccountButton;
private EditText UserEmail, UserPassword;
private TextView AlreadyHaveAccountLink;
private FirebaseAuth mAuth;
private DatabaseReference RootRef;
private ProgressDialog loadingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
RootRef = FirebaseDatabase.getInstance().getReference();
InitializeFields();
AlreadyHaveAccountLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToLoginActivity();
}
});
CreateAccountButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CreateNewAccount();
}
});
}
private void CreateNewAccount() {
String email = UserEmail.getText().toString();
String password = UserPassword.getText().toString();
if (TextUtils.isEmpty(email)){
Toast.makeText(this, "Please enter email...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this, "Please enter password...", Toast.LENGTH_SHORT).show();
}
else {
loadingBar.setTitle("Creating New Account");
loadingBar.setMessage("Please wait, while we are creating new account for you...");
loadingBar.setCanceledOnTouchOutside(true);
loadingBar.show();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
String deviceToken = FirebaseInstanceId.getInstance().getToken();
String currentUserID = mAuth.getCurrentUser().getUid();
RootRef.child("Users").child(currentUserID).setValue("");
RootRef.child("Users").child(currentUserID).child("device_token")
.setValue(deviceToken);
SendUserToMainActivity();
Toast.makeText(RegisterActivity.this, "Account Created Successfully...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
else {
String message = task.getException().toString();
Toast.makeText(RegisterActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
private void InitializeFields() {
CreateAccountButton = (Button) findViewById(R.id.register_button);
UserEmail = (EditText) findViewById(R.id.register_email);
UserPassword = (EditText) findViewById(R.id.register_password);
AlreadyHaveAccountLink = (TextView) findViewById(R.id.already_have_account_link);
loadingBar = new ProgressDialog(this);
}
private void SendUserToLoginActivity() {
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(loginIntent);
}
private void SendUserToMainActivity() {
Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
}
Может кто угодно помогите мне?
введите описание изображения здесь