2 clcurie 11 clcurie_11 于 2013.05.14 11:41 提问

FragmentManagerImpl.popBackStackImmediate() 中的 NPE

当我在电话上快速的按 back 键时,会出现下面的异常:

java.lang.NullPointerException
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1378)
at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:437)
at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:452)
at android.app.Activity.onBackPressed(Activity.java:2123)
at com.swipemaster.activities.CommonActivity.onBackPressed(CommonActivity.java:16)
at com.swipemaster.activities.MainActivity.access$0(MainActivity.java:1)
at com.swipemaster.activities.MainActivity$1.run(MainActivity.java:62)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

我在 Android 4.0.4 和 2 的设备上测试:Sony Xperia MT27i 和 ST23i。在2.2.1 和 Galaxy Mini 上不存在没有问题。如何解决这个异常问题呢?
CommonActivity 类:

public class CommonActivity extends Activity
{
    @Override
    public void onBackPressed()
    {
        MusicPlayer.continueMusic = true;
        super.onBackPressed(); // this is CommonActivity.java:16
    }

1个回答

LiDaming_and
LiDaming_and   2013.05.14 17:34

当你快速点击两次手机的返回键时,onBackPressed() 方法也会被调用2次,这就是问题之所在。
解决方法:

private boolean backAlreadyPressed = false;

@Override
public void onBackPressed()
{
    if (backAlreadyPressed)
    {
        return;
    }
    backAlreadyPressed = true;

    // old implementation of onBackPressed()
}
Csdn user default icon
上传中...
上传图片
插入图片