我已经研究了很多这个问题。我实际上完美地工作了,但现在它突然不起作用。
我正在尝试使用联系人选择器检索联系人的姓名和号码。我想获取名称和号码,并在我的应用中使用它。
我使用了这个问题答案中的代码: Selecting a number from user with multiple numbers when using the contact picker
当我尝试打开联系人选择器时,我得到一个nullPointerException。
这是我的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri result = data.getData(); //this is line 71
// get the phone number id from the Uri
String id = result.getLastPathSegment();
// query the phone numbers for the selected phone number id
Cursor c = getContentResolver().query(Phone.CONTENT_URI, null, Phone._ID + "=?", new String[]{id}, null);
int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int nameIdx = c.getColumnIndex(Phone.DISPLAY_NAME);
if(c.getCount() == 1) { // contact has a single phone number
// get the only phone number
if(c.moveToFirst()) {
phoneNumber = c.getString(phoneIdx);
contact = c.getString(nameIdx);
//set text of view to name and number
contactInfo.setText(contact + ": " + phoneNumber);
} else {
Toast.makeText(getApplicationContext(), "There are no contacts to choose from.", Toast.LENGTH_SHORT).show();
}
}
}
logcat的:
01-02 17:42:53.005: E/AndroidRuntime(3509): Uncaught handler: thread main exiting due to uncaught exception
01-02 17:42:53.025: E/AndroidRuntime(3509): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=0, data=null} to activity {com.MyActivity/com.mystuff.MyActivity}: java.lang.NullPointerException
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.ActivityThread.deliverResults(ActivityThread.java:3329)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3371)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.ActivityThread.access$2700(ActivityThread.java:119)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1893)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.os.Looper.loop(Looper.java:123)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-02 17:42:53.025: E/AndroidRuntime(3509): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 17:42:53.025: E/AndroidRuntime(3509): at java.lang.reflect.Method.invoke(Method.java:521)
01-02 17:42:53.025: E/AndroidRuntime(3509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-02 17:42:53.025: E/AndroidRuntime(3509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-02 17:42:53.025: E/AndroidRuntime(3509): at dalvik.system.NativeStart.main(Native Method)
01-02 17:42:53.025: E/AndroidRuntime(3509): Caused by: java.lang.NullPointerException
01-02 17:42:53.025: E/AndroidRuntime(3509): at com.emergencydialer.MainEmergencyDialerActivity.onActivityResult(MyActivity.java:71)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.Activity.dispatchActivityResult(Activity.java:3828)
01-02 17:42:53.025: E/AndroidRuntime(3509): at android.app.ActivityThread.deliverResults(ActivityThread.java:3325)
我在代码中用注释标记了第71行。这令人沮丧,因为这段代码昨天完美运作。不知道我做了什么,不知道为什么数据返回null。有什么想法吗?
答案 0 :(得分:0)
在处理返回的数据之前,你应该检查你的resultCode(RESULT_CANCELED或RESULT_OK:http://developer.android.com/reference/android/app/Activity.html#RESULT_CANCELED)和数据是否为空。
您还应该检查抛出startActivityForResult
的位置