【问题标题】:Get a number from your contact list从您的联系人列表中获取一个号码
【发布时间】:2014-11-02 02:24:20
【问题描述】:

我正在尝试通过 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)

【问题讨论】:

    标签: android android-contacts android-cursor


    【解决方案1】:

    问题是,我看到的代码有效,但显示了所有联系人,我只需要选择的。在这里,在同一个问题中,代码对我有用:

    Get selected phone number from contacts list without READ_CONTACTS permission

    【讨论】:

      【解决方案2】:

      首先,确保您选择的联系人有一个联系人号码。这可以通过将android.provider.ContactsContract.Contacts.HAS_PHONE_NUMBER 放在resolver.query() 的第三个参数上来完成。

      第二次尝试用ContactsContract.CommonDataKinds.Phone.NUMBER 代替ContactsContract.PhoneLookup.NUMBER

      【讨论】:

      • 仍然,它会根据您指出的更改引发异常:(
      猜你喜欢
      • 2017-06-22
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      相关资源
      最近更新 更多