查找调用标签时是否出现java.lang.NullPointerException?

时间:2012-01-09 16:41:57

标签: android nullpointerexception

我们的应用程序每周都会收到一些错误报告,并在onActivityResult()上出现此NullPointerException错误。我们已经在很多手机上测试了它并尝试修复我们的代码,但我们仍然从Dev Console获取报告。

这是错误:

  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()获取Call Label:

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

3 个答案:

答案 0 :(得分:0)

如果列“data2”可以为null,则说明异常。一个好的模式是将条件写为

"1".equals(typeLabel)

而不是

typeLabel.equals("1")

因为前者也适用于typeLabel == null。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

也许你的光标是空的? 那就是getContact(data.getData());崩溃并返回null。

我无法看到该方法的实现,所以我无法确定,但我建议在尝试访问游标之前检查NULL。