我有一个活动说A,开始时需要额外的东西。问题是,当使用 setDisplayHomeAsUpEnabled(true); 时,在另一个活动上按下后退按钮时说B 我收到错误。错误显然是由于没有从B发送额外内容。当使用 setDisplayHomeAsUpEnabled(true);
时,如何使用后退按钮将附加内容从B发送到A.活动A从主要活动
开始protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subscreen);
Bundle extras = getIntent().getExtras();
titleTask = extras.getString(DatabaseHelper.COLUMN_TITLE);
titleid = extras.getLong(DatabaseHelper.COLUMN_ROWID);
actionBar = getSupportActionBar();
actionBar.setTitle(titleTask);
actionBar.setDisplayHomeAsUpEnabled(true);
}
从B
按下后退按钮时,记录错误如下04-26 12:39:54.355: E/AndroidRuntime(15573): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.group.RedPanda.FinalProject/com.group.RedPanda.FinalProject.SubScreen}: java.lang.NullPointerException
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.os.Looper.loop(Looper.java:123)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-26 12:39:54.355: E/AndroidRuntime(15573): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 12:39:54.355: E/AndroidRuntime(15573): at java.lang.reflect.Method.invoke(Method.java:521)
04-26 12:39:54.355: E/AndroidRuntime(15573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-26 12:39:54.355: E/AndroidRuntime(15573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-26 12:39:54.355: E/AndroidRuntime(15573): at dalvik.system.NativeStart.main(Native Method)
04-26 12:39:54.355: E/AndroidRuntime(15573): Caused by: java.lang.NullPointerException
04-26 12:39:54.355: E/AndroidRuntime(15573): at com.group.RedPanda.FinalProject.SubScreen.onCreate(SubScreen.java:29)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 12:39:54.355: E/AndroidRuntime(15573): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:2)
试试这个......你真正想做的事情就是这样:
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
这将带您进入父活动,而不只是通过后台堆栈。我还添加了Intent.Flag以清除后台堆栈,当你进入家庭活动时它是一个很有用的,当用户使用“向上”按钮时可以阻止后台陷入困境