就每个按钮如何对待活动而言?有什么区别?
我有 3 个活动(我们称它们为 A、B 和 C)。A 是 B 的父级,B 是 C 的父级,并且一个意图需要额外的到下一个活动。我从 A 到 B,然后从 B 到 C。当我尝试使用向上按钮返回时,应用程序崩溃,这是因为它试图从意图中的额外信息中获取信息。
但是当我使用后退按钮时,它可以工作(???),我不知道为什么。
我试图使 Up Button 像 Back Button 一样工作,执行以下操作:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
并且也尝试过使用NavUtils.navigateUpFromSameTask(this)
,但它也不起作用。我该如何解决?
编辑:错误日志
2019-01-09 03:45:43.468 29326-29326/com.jvponte.maosdadasv1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jvponte.maosdadasv1, PID: 29326
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jvponte.maosdadasv1/com.jvponte.maosdadasv1.UserActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jvponte.maosdadasv1.User.getUsername()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6568)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jvponte.maosdadasv1.User.getUsername()' on a null object reference
at com.jvponte.maosdadasv1.UserActivity.onCreate(UserActivity.java:67)
at android.app.Activity.performCreate(Activity.java:7016)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6568)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
编辑:我在onCreate()
活动 B ( UserActivity
)中有更多代码。额外的意图被添加到活动 A
Intent intent = getIntent();
if(intent.hasExtra("clickedUser")){
mOtherUser = intent.getParcelableExtra("clickedUser");
}
if(intent.hasExtra("loggedUser")){
mLoggedUser = intent.getParcelableExtra("loggedUser");
}
/.../
mUsernameTV.setText(mOtherUser.getUsername());
mUserInfoTV.setText(mOtherUser.getUserInfo());
这里我称活动 C ( ChatActivity
)
Intent intent = new Intent(UserActivity.this, ChatActivity.class);
intent.putExtra("loggedUser", mLoggedUser);
intent.putExtra("clickedUser", mOtherUser);
startActivityForResult(intent, RC_CHAT);
在ChatActivity
onCreate()
我有这个
Intent intent = getIntent();
if (intent.hasExtra("clickedUser")){
otherUser = intent.getParcelableExtra("clickedUser");
otherUserId = otherUser.getUid();
otherUserName = otherUser.getUsername();
}
if (intent.hasExtra("loggedUser")){
loggedUser = intent.getParcelableExtra("loggedUser");
loggedUserId = loggedUser.getUid();
}
该错误可能是未正确管理此 loggedUser 和 otherUser extra。最奇怪的是,使用后退按钮 WORKS,而将 Up Button 设置为像 Back Button 一样工作却不行