我正试图通过Intent访问我的联系人列表以获取号码,但我正在设置例外。
java.lang.IllegalStateException:无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保Cursor已正确初始化。
这是我的代码:
// In some function
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, Constants.PICK_CONTACT);
}
public void onActivityResult (int requestCode, int resultCode, Intent data) {
Cursor cursor = null;
if (resultCode == RESULT_OK && requestCode == Constants.PICK_CONTACT){
try {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String number = c.getString(c.getColumnIndex(ContactsContract.PhoneLookup.NUMBER));
// do some stuff
}
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
Log.i("Excepetion", errors.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
堆栈跟踪就是这个:
11-02 03:17:36.469: I/Excepetion(25527): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
11-02 03:17:36.469: I/Excepetion(25527): at android.database.CursorWindow.nativeGetString(Native Method)
11-02 03:17:36.469: I/Excepetion(25527): at android.database.CursorWindow.getString(CursorWindow.java:434)
11-02 03:17:36.469: I/Excepetion(25527): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-02 03:17:36.469: I/Excepetion(25527): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
11-02 03:17:36.469: I/Excepetion(25527): at com.example.example.SendPrivateInfoActivity.onActivityResult(SendPrivateInfoActivity.java:76)
11-02 03:17:36.469: I/Excepetion(25527): at android.app.Activity.dispatchActivityResult(Activity.java:5322)
11-02 03:17:36.469: I/Excepetion(25527): at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
11-02 03:17:36.469: I/Excepetion(25527): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
11-02 03:17:36.469: I/Excepetion(25527): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
11-02 03:17:36.469: I/Excepetion(25527): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
11-02 03:17:36.469: I/Excepetion(25527): at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 03:17:36.469: I/Excepetion(25527): at android.os.Looper.loop(Looper.java:137)
11-02 03:17:36.469: I/Excepetion(25527): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-02 03:17:36.469: I/Excepetion(25527): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 03:17:36.469: I/Excepetion(25527): at java.lang.reflect.Method.invoke(Method.java:525)
11-02 03:17:36.469: I/Excepetion(25527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-02 03:17:36.469: I/Excepetion(25527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-02 03:17:36.469: I/Excepetion(25527): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
首先,确保您选择的联系人有联系号码。这可以通过将android.provider.ContactsContract.Contacts.HAS_PHONE_NUMBER
放在resolver.query()的第三个参数上来完成。
然后尝试ContactsContract.CommonDataKinds.Phone.NUMBER
代替ContactsContract.PhoneLookup.NUMBER
。
答案 1 :(得分:0)
问题是,我见过的代码工作但所有联系人都显示出来,我只需要选中。在同一个问题中,代码对我有用:
Get selected phone number from contacts list without READ_CONTACTS permission