2014-11-02 48 views
0

我正在嘗試通過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

首先,確保你選擇一個聯繫人的聯繫number.This可以通過將完成android.provider.ContactsContract.Contacts.HAS_PHONE_NUMBER有關resolver.query()的第三個參數。

其次嘗試ContactsContract.CommonDataKinds.Phone.NUMBER代替ContactsContract.PhoneLookup.NUMBER

+0

,它仍然拋出與變化異常,你指出:( – Fernando 2014-11-02 11:52:17

相關問題