我正在使用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)
答案 0 :(得分:2)
此错误不是因为电源中断。这是因为您要将null
转换为integer
将您的解析代码保留在try catch中,然后验证..
我在这里展示example
。请检查此
try{
String str = "";
if(!str.equals(""))
{
int n = Integer.parseInt(str);
System.out.println(n);
}
}
catch (Exception e) {
e.printStackTrace();
}