0

我正在制作一个应用程序,但我需要一些帮助。在活动的 oncreate 实践中,我有以下代码:

try{

    Bundle extras = getIntent().getExtras();
    check = extras.getInt("checkpoint1");

}catch(NumberFormatException nfe){

    check = 0;

}

在 oncreate 中,您尝试从另一个活动中获取变量检查的值,如果尝试不成功,则必须将此变量设置为零。但是现在编写的代码给了我错误。你可以帮帮我吗?

你的帮助给了我同样的错误:

E/AndroidRuntime(3028): FATAL EXCEPTION: main
E/AndroidRuntime(3028): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.BisMagdev.notesRainbow/it.BisMagdev.notesRainbow.livello4}: java.lang.NullPointerException
E/AndroidRuntime(3028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(3028):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(3028):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(3028):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(3028):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(3028):     at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(3028):     at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(3028):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3028):     at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(3028):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(3028):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(3028):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(3028): Caused by: java.lang.NullPointerException
E/AndroidRuntime(3028):     at it.BisMagdev.notesRainbow.livello4.onCreate(livello4.java:60)
E/AndroidRuntime(3028):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(3028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(3028):     ... 11 more
4

6 回答 6

3

您可以设置默认返回值,无需 try-catch:

Bundle extras = getIntent().getExtras();
check = extras.getInt("checkpoint1", 0);

此外,getInt不会抛出NumberFormatException,所以你无法捕捉到那个异常......

于 2012-08-25T17:16:00.803 回答
0

不认为这些语句需要 try/catch 块......所以删除它们并尝试......

Bundle extras = getIntent().getExtras();
check = extras.getInt("checkpoint1");

但是,您可以"checkpoint1"在将其分配给check变量之前检查它的值。

if ((extras.getInt("checkpoint1"))!=0){

        check = extras.getInt("checkpoint1");

 }
  else{

         check = 0;
 }
于 2012-08-25T17:17:38.807 回答
0

我认为你可以做到

if getIntent().hasExtra("checkpoint") { // this checks for the extra...
    Bundle extras = getIntent().getExtras();
    if (null != getIntent().getExtras()){
        check = extras.getInt("checkpoint", defaultValue); // where default is non zero
        check = extras.getInt("checkpoint"); // will be zero by default if checkpoint is not found
    } else {
        // There are no extras associated with this intent
    }
} else {
    // checkpoint not contained in the extras...
}
于 2012-08-25T17:18:54.890 回答
0

You have to change NumberFormatException in Throwable so you are sure you catch NullPointerException too.

于 2012-08-27T09:51:27.563 回答
0

livello4.java 的第 60 行是什么?我怀疑 getExtras() 返回的包是空的。

于 2012-08-25T17:39:57.520 回答
0

你捕获了 numberformatexception 但你得到了一个空指针异常。您也必须捕获空指针异常,或者您只需捕获异常。这意味着当出现问题时,您可以将变量设置为 0。

于 2012-08-25T17:50:13.113 回答