1

我完全被这个难住了。我有一个活动 C,当我尝试按下后退按钮时,它可以工作。但是当我使用操作栏中的主页/向上按钮时,它会崩溃(请参阅以下错误)。这是我的代码中处理后退/向上按钮的部分。

活动 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?后退按钮仍然有效的事实完全让我感到困惑。

4

1 回答 1

3

通常不建议调用 Activity 生命周期方法。这些onWhatEver方法由框架使用。如果您想完成活动,请使用...

this.finish()

或者 ...

getActivity.finish()

如果您处于片段上下文中。

pssonBackPressed确实有效(与此答案的初始假设相反)。有关此问题的根本原因,请参阅下面的评论。

ps这篇文章提出this.dispatchKeyEvent(new Keyevent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));了一个替代方案

于 2013-08-22T08:30:18.253 回答