我的应用程序从登录后活动切换为应用程序中的个人资料活动时崩溃。 构建完成,没有错误。
任何想法为何? (感谢您的帮助)
///////////////////////////////////////////////// //////////////////////////// ProfileActivity ---->
package com.smarthelp.smarthelp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
public class ProfileActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private Button logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
firebaseAuth= FirebaseAuth.getInstance();
logout = (Button)findViewById(R.id.btnLogout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logout();
}
});
configureibtnHelp();
}
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
}
private void configureibtnHelp() {
ImageButton ibtnHelp = (ImageButton) findViewById(R.id.ibtnHelp);
ibtnHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profile_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.logoutMenu:{
Logout();
}
}
return super.onOptionsItemSelected(item);
}
}
///////////////////////////////////////////////// //////////////////////// HelpActivity ---->
package com.smarthelp.smarthelp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
public class HelpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
configureibtnProfile();
}
private void configureibtnProfile(){
ImageButton ibtnProfile = (ImageButton) findViewById(R.id.ibtnProfile);
ibtnProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(HelpActivity.this, ProfileActivity.class));
}
});
}
}
///////////////////////////////////////////////// /////////////////////////////// activity_help.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=".HelpActivity">
<ImageButton
android:id="@+id/ibtnProfile"
android:layout_width="51dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="ProfilButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="@drawable/profil" />
<ImageButton
android:id="@+id/ibtnOrder"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="OrderButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="@drawable/order" />
<ImageButton
android:id="@+id/ibtnHelp"
android:layout_width="51dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="236dp"
android:layout_marginRight="8dp"
android:layout_marginStart="236dp"
android:layout_marginTop="8dp"
android:contentDescription="HelpButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.875"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="@drawable/help" />
</android.support.constraint.ConstraintLayout>
///////////////////////////////////////////////// //////////////////////////////// -------> Logcat
08-13 19:13:15.273 21068-21068/com.smarthelp.smarthelp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smarthelp.smarthelp, PID: 21068
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarthelp.smarthelp/com.smarthelp.smarthelp.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.smarthelp.smarthelp.ProfileActivity.configureibtnHelp(ProfileActivity.java:48)
at com.smarthelp.smarthelp.ProfileActivity.onCreate(ProfileActivity.java:35)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
答案 0 :(得分:2)
从崩溃日志看来
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
您尝试为其设置onclick侦听器的按钮上有一个NPE。 在您的ProfileActivity中,将按钮设置为XML的ImageButtons之一。 假设您正在尝试为第一个图像按钮
只需在设置onClickListener之前添加以下行
logout=(Button) findViewById(R.id.ibtnProfile);
答案 1 :(得分:0)
更改
firebaseAuth.getInstance();
到
FirebaseAuth.getInstance();
ProfileActivity.java第14行
答案 2 :(得分:0)
在堆栈跟踪中有很多说法:尝试在第27行的addOnClickListener
的空对象上调用ProfileActivity
您尚未使用任何值初始化logout
按钮。可能忘记了findViewById
。