2

当我在手机上快速按回、回、回时,出现以下异常:

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
    }
4

2 回答 2

1

据我了解,问题return popBackStackState(mActivity.mHandler, null, -1, 0);出在以下函数的调用中:

 @Override
 public boolean popBackStackImmediate() {
     checkStateLoss();
     executePendingTransactions();
     return popBackStackState(mActivity.mHandler, null, -1, 0);
 }

来自安卓源

所以,我看到的真正问题是:为什么mActivity该函数(或某些特定于供应商的字段)为 null?

根据评论,该问题仅在signle设备上观察到,因此我可以提出以下建议:

  • 在其他设备上验证问题以检测它是否可重现(因为库存 android 4.0.4 中没有代码FragmentManager.java:457);
  • Try to build app for API 15 or API 16 and check if issue remain;
  • Based on this question replies, the problem might be in one of onDestroy() methods;
  • Investigate applications code to find out if some part of it calls finish() for all activities or does some handling to exit from the app (e.g. tries to handle multiple back presses as exit from app);
  • Try to workout some hack (e.g. handling of back with small 100ms delay). But for this way I would suggest You to be sure that the issue is vendor-specific and put that hack under check of Build.MANUFACTURER;
于 2013-03-30T12:15:33.377 回答
1

When you quickly press 2 times back button on your device, the onBackPressed() method is also called 2 times. This is causing the problem.

Solution:

Look into all your onBackPressed() methods. Do you have something, that can cause problems, because of being called 2 times? For example, this can be a call to postDelayed(), which was mine case. If yes, add a flag assuring that your onBackPressed() body will be executed only once, despite the number of back button clicks:

private boolean backAlreadyPressed = false;

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

    // old implementation of onBackPressed()
}
于 2013-03-30T14:38:53.600 回答