【问题标题】:properly handle the try {} catch {}正确处理 try {} catch {}
【发布时间】:2012-08-25 17:12:24
【问题描述】:

我正在制作一个应用程序,但我需要一点帮助。在活动的 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

【问题讨论】:

  • getIntent() 返回启动活动的意图。启动 Activity 的 Intent 可能有一个 Bundle 与之关联的附加组件。 getIntent().getExtras();
  • 所以我可以试试这个方法:Bundle extras = getIntent().getExtras(); if (额外 == null ){ bubu = 0; }else{ bubu = extras.getInt("checkpoint1"); }
  • 是的,但是这段代码可能不太容易出错... Int bubu = DEFAULT_BUBU_VALUE; // 其中 DEFAULT_BUBU_VALUE 为零或一些非零默认 Bundle extras = getIntent().getExtras(); if (null != extras){ bubu = extras.getInt("checkpoint1", DEFAULT_BUBU_VALUE); } 调用 getInt("checkpoint1");将导致默认返回零...

标签: android android-layout android-intent android-widget


【解决方案1】:

可以设置默认返回值,不需要try-catch:

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

此外,getInt 不会抛出 NumberFormatException,因此您无法捕获该异常...

【讨论】:

    【解决方案2】:

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

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

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

    if ((extras.getInt("checkpoint1"))!=0){
    
            check = extras.getInt("checkpoint1");
    
     }
      else{
    
             check = 0;
     }
    

    【讨论】:

    • getInt("checkpoint1") 与 getInt("checkpoint1", 0) 相同,即 getInt 默认返回值为零
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我觉得你可以的

      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...
      }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          您必须更改 Throwable 中的 NumberFormatException,以便确保您也捕获 NullPointerException。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-01-03
            • 1970-01-01
            • 1970-01-01
            • 2011-04-01
            • 1970-01-01
            • 2019-04-23
            • 2023-03-03
            相关资源
            最近更新 更多