1

当我按下电源按钮锁定屏幕并再次按下它以解锁屏幕时,应用程序崩溃。屏幕解锁后应用程序崩溃。

这是我在 logcat 中得到的 stackTress:

05-09 18:46:57.254: E/AndroidRuntime(25354):    FATAL EXCEPTION: main
05-09 18:46:57.254: E/AndroidRuntime(25354):    java.lang.NullPointerException
05-09 18:46:57.254: E/AndroidRuntime(25354):    at org.andengine.ui.activity.BaseGameActivity.onResumeGame(BaseGameActivity.java:222)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at org.andengine.ui.activity.BaseGameActivity$4.run(BaseGameActivity.java:373)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at android.os.Handler.handleCallback(Handler.java:615)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at android.os.Looper.loop(Looper.java:137)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at android.app.ActivityThread.main(ActivityThread.java:4744)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at java.lang.reflect.Method.invoke(Method.java:511)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-09 18:46:57.254: E/AndroidRuntime(25354):    at dalvik.system.NativeStart.main(Native Method)

BaseGameActivityClass 的 onResumeGame() 方法出现 Nullpointer 异常。

我在清单的“活动”标签中添加了这一行

 android:configChanges="orientation|keyboard|keyboardHidden"

在我扩展 BaseGameActivity 的 GameActivity 中,我添加了这个

    @Override
    public void onPause() {
      super.onPause();
      mEngine.stop();
    }

    @Override
    protected void onResume() {
    // The activity has become visible (it is now "resumed").
       super.onResume();
       mEngine.start();
    }

怎么了?我应该怎么做才能停止在电源按钮问题上崩溃的游戏?

4

1 回答 1

2

Whenever onResume() method is called it again recreate all your resources. So at the time of your onResume() method call the controller does not found mEngine instance. Because of this it fires an error NullPointerException.

The task of onPause() method is to stop the music and sound and onResume() method is to again play sound and music. These methods are not used for any other purpose in android game development particularly in AndEngine.

于 2013-05-10T19:21:13.447 回答