我們的應用程序每週都會在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;
}
}
}
你可以發佈什麼在線PhoneLog.java:555 \ – nandeesh 2012-01-09 16:44:30
555行是'if(typeLabel.equals(「1」)){' – user961389 2012-01-09 16:53:18
爲什麼你不檢查每個空值如果或只是做默認當你得到typelabel爲空的行爲。有些聯繫人可能沒有data2 – nandeesh 2012-01-09 16:57:08