Android强制关闭powerInterrupt时出错

问题描述:

我正在使用andengine开发android游戏。 问题是什么时候断电或锁屏我正在强制关闭错误。 我的代码为N logcat的错误是如下:Android强制关闭powerInterrupt时出错

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) 
+0

我不这么认为。 GameActivity:java.lang.NumberFormatException:无法解析'空白'作为整数 –

此错误不是电源中断的原因。这是因为你正在转换nullinteger

让您解析代码在尝试捕捉,然后验证..

我在这里展示example。查这个

try{ 
    String str = ""; 
    if(!str.equals("")) 
    { 
     int n = Integer.parseInt(str); 
     System.out.println(n); 
    } 
} 
catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

感谢它现在工作正常.. – Johnson