这个问题我研究了很多。我实际上已经完美地工作了,但现在它突然不起作用了。
我正在尝试使用联系人选择器检索联系人的姓名和号码。我想获取名称和号码并在我的应用程序中使用它。
我使用了这个问题的答案中的代码: 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();
}
}
}
日志猫:
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。有任何想法吗?