0

我正在使用andengine 开发安卓游戏。问题是当电源中断或屏幕锁定时我强制关闭错误。我的代码 n logcat 错误如下:

 public class ScreenReceiver  extends BroadcastReceiver{


        @Override
        public void onReceive(Context context, Intent intent) {
             if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                 wasScreenOn=true;
             }else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT )){
                 wasScreenOn=true;
             }
             if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                            wasScreenOn=false;
                               }
                            }

//onresume方法

 protected void onResume() {
     if(wasScreenOn){
       RESUME GAME
                    }
           }

LOGCAT 错误:

   12-21 12:26:07.016: ERROR/AndroidRuntime(1220): FATAL EXCEPTION: main
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m2f.StepUpPuzzle/com.m2f.StepUpPuzzle.GameActivity}: java.lang.NumberFormatException: unable to parse 'null' as integer
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1740)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1759)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2938)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.app.ActivityThread.access$1600(ActivityThread.java:155)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1003)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.os.Handler.dispatchMessage(Handler.java:130)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.os.Looper.loop(SourceFile:351)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at android.app.ActivityThread.main(ActivityThread.java:3826)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at java.lang.reflect.Method.invoke(Method.java:538)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
    12-21 12:26:07.016: ERROR/AndroidRuntime(1220):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

这个错误不是因为电源中断。这是因为您正在转换nullinteger

将您的解析代码保留在尝试捕获中,然后验证..

我在这里展示example。检查这个

try{
    String str = "";
    if(!str.equals(""))
    {
        int n = Integer.parseInt(str);
        System.out.println(n);
    }
}
catch (Exception e) {
    e.printStackTrace();
}
于 2012-12-21T07:10:45.797 回答