Android后退操作按钮导致应用程序崩溃

Android后退操作按钮导致应用程序崩溃

本文介绍了Android后退操作按钮导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个新的应用程序,该应用程序由3个活动组成: -启动画面 -活动A -活动B,以及 -活动C

I am building a new application which composed of 3 activities namely: - Splash Screen - Activity A - Activity B, and - Activity C

从活动中,用户可以进入以下两个活动:

From activity A user can go to both activities indicated below:

A-> B-> C(从行为A用户可以先到B再到C).A-> C(行为A用户可以直接转到C).B-> C(从B用户可以转到C).

A -> B -> C (from act A user can go to B then to C).A -> C (from act A user can go straight to C).B -> C (from B user can go to C).

我还在活动之间传递了Serializable Intent Extra.

i also pass Serializable intent Extra between activities.

我遇到的问题是,每当我按下操作栏上的后退按钮(左上角)时,它总是使我的应用崩溃(错误:NULL指针异常).

The problem that i am having is whenever i pressed the back button on the Action Bar (Top Left Corner) it always makes my app crashes (Error: NULL Pointer Exception).

我试图将这段代码放在我的所有活动中.

I have tried to place this code on ALL of my activity.

@Override
public void onBackPressed() {
    this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}

我试图以某种方式模仿物理后退按钮的行为,因为当用户按下物理后退按钮时,它可以正常工作.但是也会引发一些错误.

I tried to somehow mimic the physical backbutton behaviour since it is working when user press physical backbutton. But somewhat throws error as well.

public void onBackPressed(){
  super.onBackPressed();
}

或(或者,从字面上重新启动应用程序,这是令人沮丧的,因为它会从启动屏幕重新启动应用程序.)

or (well this one literally restart the app, which is discourage since it restart the app from splash screen).

public void onBackPressed(){
  super.onBackPressed();
  finish();
}

有人知道实现后退按钮的适当方法吗?

Does anyone know the appropriate way to implement back button?

推荐答案

左上角的按钮不是返回"按钮,而是向上"按钮,而只是操作栏中的按钮,onBackPressed表示被按下的硬件后退按钮.向上和向后导航不一定相同(向后"表示转到我以前的位置,而向上"表示转到应用程序层次结构的上层).看看 http://developer.android.com/design/patterns/navigation.html 了解更多信息.

The button on the top left corner is not the "Back" button, it would be the "up" button, and it's just a button in the action bar, onBackPressed refers to the hardware back button being pressed.Navigation with back and up is not necessarily the same ("back" means go to where I was before, whereas "up" means go to the upper level in the app hierarchy).Take a look at http://developer.android.com/design/patterns/navigation.html for more information.

(此外,请尽量避免显示启动画面,在Android设计模式中强烈建议不要使用它们)

(Also, try to avoid a splash screen, they are highly discouraged in android design patterns)

我忘了提到如何实际处理向上"按钮.您可以在活动的onOptionsItemSelected上执行此操作:

I forgot to mention how to actually handle the "up" button. You do that on your activity's onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        // Handle "up" button behavior here.
        return true;
    } else {
        // handle other items here
    }
    // return true if you handled the button click, otherwise return false.
}

这篇关于Android后退操作按钮导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:39