2012-01-09 93 views
0

我們的應用程序每週都會在onActivityResult()上得到一些錯誤報告,並且這個NullPointerException錯誤。我們已經在很多手機上測試過它,並試圖修復我們的代碼,但我們仍然從開發控制檯獲取報告。尋找呼叫標籤時發現java.lang.NullPointerException?

以下是錯誤:下面

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/612 flg=0x1 (has extras) }} to activity {com.package.app/com.package.app.PhoneLog}: java.lang.NullPointerException 
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536) 
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578) 
at android.app.ActivityThread.access$2000(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:3691) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at com.package.app.PhoneLog..onActivityResult(PhoneLog.java:555) 
at android.app.Activity.dispatchActivityResult(Activity.java:3934) 
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 
... 11 more 

我認爲有錯誤的地方,我們從onActivityResult(獲得呼叫標籤來):

 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
      { 
       super.onActivityResult(requestCode, resultCode, data); 
       if (resultCode == RESULT_OK) { 
       switch(requestCode) { 
        case PICK_CONTACT: 
         // handle the contact result 


       Cursor c = getContact(data.getData()); 
         if (c.moveToFirst()) 
         { 
          String name = c.getString(c.getColumnIndex("display_name")); 
          String num = c.getString(c.getColumnIndex("data1")); 
          typeLabel = c.getString(c.getColumnIndex("data2")); 
          long l = c.getInt(c.getColumnIndex("contact_id")); 
          c.getString(c.getColumnIndex("custom_ringtone")); 


         //error is here Caused by: java.lang.NullPointerException    //at PhoneLog.onActivityResult(PhoneLog.java:555) 
          if(typeLabel.equals("1")) { 
           typeLabel = "Home"; 
           } else if(typeLabel.equals("2")) { 
            typeLabel = "Mobile"; 
            } else if(typeLabel.equals("3")) { 
             typeLabel = "Work"; 
            } else { 
             typeLabel = ""; 
            } 



          callerName.setText(name); 
          callerNum.setText(num); 
          setChosenContactId(l); 


         } 
        break; 
    } 
    } 
    } 
+0

你可以發佈什麼在線PhoneLog.java:555 \ – nandeesh 2012-01-09 16:44:30

+0

555行是'if(typeLabel.equals(「1」)){' – user961389 2012-01-09 16:53:18

+0

爲什麼你不檢查每個空值如果或只是做默認當你得到typelabel爲空的行爲。有些聯繫人可能沒有data2 – nandeesh 2012-01-09 16:57:08

回答

0

如果列「數據2」能爲空解釋異常。一個很好的模式是寫的條件

"1".equals(typeLabel) 

,而不是

typeLabel.equals("1") 

因爲前者的作品也爲類型標== NULL。

+0

所以我應該這樣重新編碼: 'if(typeLabel == null){ typeLabel =「」;其他如果(「1」.equals(typeLabel)){typelabel =「Home」; (「2」.equals(typeLabel)){type = {「Mobile」;}}; (「3」.equals(typeLabel)){typelabel =「Work」; } else { typeLabel =「」; }' – user961389 2012-01-09 17:07:32

+0

如果typeLabel爲null導致異常,那麼這將解決問題。您可以跳過第一個條件(檢查typeLabel == null),因爲該案例與else-path重疊,並且typeLabel爲null是沒有問題的,因爲在字符串文字上調用equals()(「1」,「2」 ,「3」)。 – Stefan 2012-01-10 09:01:11

0

改變這種希望它將工作

if (resultCode == RESULT_OK) { 
      switch(requestCode) { 
       case PICK_CONTACT: 
        // handle the contact result 

Cursor c = getContact(data.getData()); 
        c.moveToFirst(); 

         String name = c.getString(c.getColumnIndex("display_name")); 
         String num = c.getString(c.getColumnIndex("data1")); 
         typeLabel = c.getString(c.getColumnIndex("data2")); 
         long l = c.getInt(c.getColumnIndex("contact_id")); 
         c.getString(c.getColumnIndex("custom_ringtone")); 

         c.moveTonext(); 
        //error is here Caused by: java.lang.NullPointerException    //at PhoneLog.onActivityResult(PhoneLog.java:555) 
         if(typeLabel=="1")) { 
          typeLabel = "Home"; 
          } else if(typeLabel=="2")) { 
           typeLabel = "Mobile"; 
           } else if(typeLabel=="3")) { 
            typeLabel = "Work"; 
           } else { 
            typeLabel = ""; 
           } 



         callerName.setText(name); 
         callerNum.setText(num); 
         setChosenContactId(l); 



       break; 
} 
0

也許你的光標爲空? 這就是如果getContact(data.getData());崩潰並返回null。

我不能看到方法的實現,所以我不能確定,但​​我會建議在嘗試訪問遊標之前檢查NULL。