我正在开发一个应用程序,并且在构建登录活动时,每当我按下LOGIN按钮并显示此错误时,该应用程序便崩溃了。这只是使用firebase的简单登录活动。
这是登录活动的XML代码,
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/relativeLayout"
android:background="#000000">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="124dp"
android:text="EMAIL"
android:textSize="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="@+id/name_textview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="160dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="224dp"
android:text="PASSWORD"
android:textColor="#FFFFFF"
android:textSize="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/password_textview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="260dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="#FFFFFF"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/login_button"
android:layout_width="352dp"
android:layout_height="40dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="332dp"
android:text="L O G I N"
android:textColor="#FFFFFF"
android:background="#404040"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/registration_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="112dp"
android:layout_marginStart="112dp"
android:layout_marginTop="448dp"
android:text="New User? Register Here"
android:textColor="#FFFFFF"
android:textSize="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
&这是登录活动的JAVA代码,
package com.example.theroos.loginapp;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
EditText enter_email, enter_password;
Button login_button;
TextView gotoreg_text;
FirebaseAuth firebaseAuth;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter_email=(EditText)findViewById(R.id.email_edittext);
enter_password=(EditText)findViewById(R.id.Password_editText);
login_button=(Button)findViewById(R.id.login_button);
gotoreg_text=(TextView)findViewById(R.id.registration_text);
firebaseAuth=FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
progressDialog = new ProgressDialog(this);
if (user != null){
finish();
startActivity(new Intent(MainActivity.this,DestinationActivity.class));
}
login_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate(enter_email.getText().toString(),enter_password.getText().toString());
}
});
gotoreg_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,RegistrationActivity.class));
}
});
}
private void validate (String username, String userpassword){
progressDialog.setMessage("wait for a while");
progressDialog.show();;
firebaseAuth.signInWithEmailAndPassword(username,userpassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Successfully logged in", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,DestinationActivity.class));
}
else {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "You have entered wrong email or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
&这是我在logcat中遇到的错误,
FATAL EXCEPTION: main
Process: com.example.theroos.loginapp, PID: 31527
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.theroos.loginapp.MainActivity$1.onClick(MainActivity.java:51)
at android.view.View.performClick(View.java:5609)
at android.view.View$PerformClick.run(View.java:22263)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
如果您需要帮助我找出该问题,我将不胜感激,我无法弄清login_button.setOnClickListener()
的问题。提前致谢。