每当我尝试运行应用时,我都会收到错误消息:
这是.java类:
package com.safariagaming.flix;
import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class SignUpScreen extends AppCompatActivity implements View.OnClickListener{
//defining view objects
private EditText editTextEmail;
private EditText editTextPassword;
private Button buttonSignup;
private ProgressDialog progressDialog;
//defining firebaseauth object
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_screen);
//initializing firebase auth object
firebaseAuth = FirebaseAuth.getInstance();
//initializing views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
buttonSignup = (Button) findViewById(R.id.buttonSignup);
progressDialog = new ProgressDialog(this);
//attaching listener to button
buttonSignup.setOnClickListener(this);
}
private void registerUser(){
//getting email and password from edit texts
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
//checking if email and passwords are empty
if(TextUtils.isEmpty(email)){
Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
return;
}
//if the email and password are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
//display some message here
Toast.makeText(SignUpScreen.this,"Successfully registered",Toast.LENGTH_LONG).show();
}else{
//display some message here
Toast.makeText(SignUpScreen.this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
@Override
public void onClick(View view) {
//calling register method on click
registerUser();
}
}
(我认为错误发生是因为我没有正确初始化firebase auth对象)
这是我的XML类
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:hint="Enter email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:hint="Enter password"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonSignup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Signup" />
</LinearLayout>
以下是我遇到的错误消息
03-29 18:14:49.700 9230-9230/com.safariagaming.flix E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.safariagaming.flix, PID: 9230
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(java.lang.String, java.lang.String)' on a null object reference
at com.safariagaming.flix.SignUpScreen.registerUser(SignUpScreen.java:72)
at com.safariagaming.flix.SignUpScreen.onClick(SignUpScreen.java:93)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
您应该已经在方法email
之外定义了变量password
和registerUser()
。看来你的Button也没有正确设置。
package com.safariagaming.flix;
/**
* Imports...
*/
public class SignUpScreen extends AppCompatActivity implements View.OnClickListener {
private String email = null;
private String password = null;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_screen);
/**
* Inits and code...
*/
// Signup button on listener
buttonSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
email = editTextEmail.getText().toString().trim();
password = editTextPassword.getText().toString().trim();
// Verifications
if (TextUtils.isEmpty(email)) {
Toast.makeText(LoginActivity.this, R.string.empty_email, Toast.LENGTH_SHORT).show();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
Toast.makeText(LoginActivity.this, R.string.wrong_email, Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, R.string.empty_password, Toast.LENGTH_SHORT).show();
}
// Authenticating user
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, R.string.wrong_credentials, Toast.LENGTH_SHORT).show();
} else {
goDashboard();
finish();
}
}
});
}
});
}
// onStart() state listener
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
答案 1 :(得分:0)
java.lang.NullPointerException:尝试调用虚方法 “com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(java.lang.String中, “空对象引用
上的java.lang.String)”
这意味着您的mAuth
成员变量为null。您可能从未为其分配过值。在onCreate()期间或至少在您调用createUserWithEmailAndPassword之前的某个时间为其分配mAuth = FirebaseAuth.getInstance()
。