我对这个完全感到难过。我有一个活动C,当我尝试按下后退按钮时,它可以工作。但是,当我使用操作栏中的home / up按钮时,它只会崩溃(请参阅以下错误)。以下是我的代码中处理后退/上移按钮的部分。
活动C:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offline_viewer);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(!isOnline) {
menu.removeItem(R.id.saveRoute);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.offline_viewer, menu);
return true;
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
//Get names for saving
String[] startParts = onlineFrom.split(",");
String startName = startParts[0] + "," + startParts[1];
String[] endParts = onlineTo.split(",");
String endName = endParts[0] + "," + endParts[1];
System.out.println(item.getItemId());
switch(item.getItemId()){
//Save xml file or route once pressed
case R.id.saveRoute:
//TODO:Uncomment once server is ready
new DownloadFileFromURL(this, startName, endName).execute(urlForDownload);
return true;
case android.R.id.home:
System.out.println(item.getItemId());
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
错误:
08-22 16:15:43.629: W/dalvikvm(4908): threadid=1: thread exiting with uncaught exception (group=0x41c7e888)
08-22 16:15:43.634: E/AndroidRuntime(4908): FATAL EXCEPTION: main
08-22 16:15:43.634: E/AndroidRuntime(4908): java.lang.NullPointerException
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.example.otpxmlgetter.OfflineViewer.onOptionsItemSelected(OfflineViewer.java:185)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.app.Activity.onMenuItemSelected(Activity.java:2590)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.view.View.performClick(View.java:4204)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.view.View$PerformClick.run(View.java:17354)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.os.Handler.handleCallback(Handler.java:725)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.os.Looper.loop(Looper.java:137)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.app.ActivityThread.main(ActivityThread.java:5232)
08-22 16:15:43.634: E/AndroidRuntime(4908): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:15:43.634: E/AndroidRuntime(4908): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
08-22 16:15:43.634: E/AndroidRuntime(4908): at dalvik.system.NativeStart.main(Native Method)
这是否与有两种可能的方法进入活动C这一事实有关? A-> B-> C或A-> D-> C? 后退按钮仍能正常工作的事实让我感到困惑。
答案 0 :(得分:3)
通常不建议调用Activity life cyle方法。框架使用onWhatEver
方法。如果你想完成活动而不是使用......
this.finish()
或......
getActivity.finish()
如果您处于片段上下文中。
p.s.s :onBackPressed
确实有效(反对此答案的初始推荐)。请参阅以下评论,了解此问题的根本原因。
p.s。:this post建议this.dispatchKeyEvent(new Keyevent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
替代