2013-06-20 98 views
0

我做了一個簡單的Android應用程序,我需要訪問手機中的聯繫人。無法讀取從CursorWindow行1,col -1

但是我的應用程序崩潰,我得到這個錯誤「無法讀取行1,列-1從CursorWindow」

這裏是日誌:

 
06-19 11:03:59.582: E/CursorWindow(7926): Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 29 columns. 
06-19 11:03:59.611: D/AndroidRuntime(7926): Shutting down VM 
06-19 11:03:59.611: W/dalvikvm(7926): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
06-19 11:03:59.842: E/AndroidRuntime(7926): FATAL EXCEPTION: main 
06-19 11:03:59.842: E/AndroidRuntime(7926): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.Search}: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.os.Looper.loop(Looper.java:137) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at dalvik.system.NativeStart.main(Native Method) 
06-19 11:03:59.842: E/AndroidRuntime(7926): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.database.CursorWindow.nativeGetString(Native Method) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.database.CursorWindow.getString(CursorWindow.java:434) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.database.CursorWrapper.getString(CursorWrapper.java:114) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at com.example.application.Search.onCreate(Search.java:33) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.Activity.performCreate(Activity.java:5104) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
06-19 11:03:59.842: E/AndroidRuntime(7926):  ... 11 more 

這裏是代碼:

 Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
       null, null, null, null); 
    people.moveToFirst(); 
    while(people.moveToNext()) { 
     int name = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
     String contact = people.getString(name); 

     int contact_no = people.getColumnIndex(PhoneLookup.NUMBER); 
     String number = people.getString(contact_no); 

    } 

    people.close(); 
+0

你錯過了peope.moveToFirst() – Blackbelt

+0

我也加了。但它仍然給出相同的問題 因此我現在從代碼中刪除它。 – drarkayl

+0

沒有數據庫我只是試圖訪問所有聯繫人的姓名和號碼。 – drarkayl

回答

0

而是通空作爲投影索要列則需要

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
String[] projection = new String[] { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER}; 

Cursor people = getContentResolver().query(uri, projection, null, null, null); 
+0

我試過這個但我仍然得到同樣的錯誤 – drarkayl

+0

錯誤發生在NUMBER上,它的名字可以正常工作 – drarkayl

0

作爲例外表明列號不正確。因此,從查詢返回的遊標中,無論是PhoneLookup.DISPLAY_NAME還是PhoneLookup.NUMBER列都不存在,這取決於發生異常的行。你可以明確地指定你需要從db中獲得的列,以便你知道查詢是否沒有拋出任何異常,那麼這個列肯定會存在。

相關問題