尝试在 SignUpActivity.onCreate 处的空对象引用上调用虚方法 'void android.widget.Button.setOnClickListener
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener on a null object reference at SignUpActivity.onCreate
我尝试在loginactivity中点击注册按钮,但是在app中点击按钮时,会跳出这样的错误,确实有类似我问的问题,但我确实尝试了你提出的所有解决方案,但它对我不起作用。
错误:
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.fivenine.shareit, PID: 17033
> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fivenine.shareit/com.fivenine.shareit.AccountActivity.SignUpActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
> at android.app.ActivityThread.-wrap12(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:165)
> at android.app.ActivityThread.main(ActivityThread.java:6375)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
> at com.fivenine.shareit.AccountActivity.SignUpActivity.onCreate(SignUpActivity.java:44)
> at android.app.Activity.performCreate(Activity.java:6845)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
> at android.app.ActivityThread.-wrap12(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:165)
> at android.app.ActivityThread.main(ActivityThread.java:6375)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
这里是 SignUpActivity.java:
import android.content.Intent;
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.ProgressBar;
import android.widget.Toast;
import com.fivenine.shareit.MainActivity;
import com.fivenine.shareit.R;
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 SignUpActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
btnResetPassword.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
startActivity(new Intent(SignUpActivity.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
Toast.makeText(getApplicationContext(), "Enter email address!",
Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(getApplicationContext(), "Enter password!",
Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6){
Toast.makeText(getApplicationContext(), "Password too short,enter minimum 6 characters!",
Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignUpActivity.this,"createUserWithEmail:onComplete:" + task.isSuccessful(),
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if(!task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Authenticaton faled." + task.getException(),
Toast.LENGTH_SHORT).show();
}else{
startActivity(new Intent(SignUpActivity.this,
MainActivity.class));
finish();
}
}
});
}
});
}
@Override
protected void onResume(){
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
activity_sign_up.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="com.fivenine.shareit.AccountActivity.SignUpActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/gradient_6"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:contentDescription=""
android:src="@drawable/profile"
tools:ignore="ContentDescription" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:textColor="@android:color/black"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/hint_password"
android:imeActionId="@integer/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@drawable/btn_corner_pink_gradient"
android:text="@string/action_sign_in_short"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<!-- <Button
android:id="@+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_forgot_password"
android:textAllCaps="false"
android:textColor="@color/colorAccent"/> -->
<!-- Link to login screen -->
<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_link_to_login"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:textSize="15sp"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone"/>
</android.support.constraint.ConstraintLayout>
'btn_reset_password' 按钮的代码已注释。只需取消注释按钮代码即可。
您在 xml 中评论了 btn_reset_password。所以它调用空对象引用。只需取消注释 xml 中的 btn_reset_password 并尝试。
我尝试在loginactivity中点击注册按钮,但是在app中点击按钮时,会跳出这样的错误,确实有类似我问的问题,但我确实尝试了你提出的所有解决方案,但它对我不起作用。
错误:
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.fivenine.shareit, PID: 17033
> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fivenine.shareit/com.fivenine.shareit.AccountActivity.SignUpActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
> at android.app.ActivityThread.-wrap12(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:165)
> at android.app.ActivityThread.main(ActivityThread.java:6375)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
> on a null object reference
> at com.fivenine.shareit.AccountActivity.SignUpActivity.onCreate(SignUpActivity.java:44)
> at android.app.Activity.performCreate(Activity.java:6845)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
> at android.app.ActivityThread.-wrap12(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:165)
> at android.app.ActivityThread.main(ActivityThread.java:6375)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
这里是 SignUpActivity.java:
import android.content.Intent;
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.ProgressBar;
import android.widget.Toast;
import com.fivenine.shareit.MainActivity;
import com.fivenine.shareit.R;
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 SignUpActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
btnResetPassword.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
startActivity(new Intent(SignUpActivity.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
Toast.makeText(getApplicationContext(), "Enter email address!",
Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(getApplicationContext(), "Enter password!",
Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6){
Toast.makeText(getApplicationContext(), "Password too short,enter minimum 6 characters!",
Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignUpActivity.this,"createUserWithEmail:onComplete:" + task.isSuccessful(),
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if(!task.isSuccessful()){
Toast.makeText(SignUpActivity.this, "Authenticaton faled." + task.getException(),
Toast.LENGTH_SHORT).show();
}else{
startActivity(new Intent(SignUpActivity.this,
MainActivity.class));
finish();
}
}
});
}
});
}
@Override
protected void onResume(){
super.onResume();
progressBar.setVisibility(View.GONE);
}
}
activity_sign_up.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="com.fivenine.shareit.AccountActivity.SignUpActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/gradient_6"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:contentDescription=""
android:src="@drawable/profile"
tools:ignore="ContentDescription" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:textColor="@android:color/black"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/hint_password"
android:imeActionId="@integer/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@drawable/btn_corner_pink_gradient"
android:text="@string/action_sign_in_short"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<!-- <Button
android:id="@+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_forgot_password"
android:textAllCaps="false"
android:textColor="@color/colorAccent"/> -->
<!-- Link to login screen -->
<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_link_to_login"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:textSize="15sp"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone"/>
</android.support.constraint.ConstraintLayout>
'btn_reset_password' 按钮的代码已注释。只需取消注释按钮代码即可。
您在 xml 中评论了 btn_reset_password。所以它调用空对象引用。只需取消注释 xml 中的 btn_reset_password 并尝试。