我已經設置了一個光標,我想要返回具有電話號碼的聯繫人的列表視圖。因此,在每個單元格中,我都會有聯繫人姓名和電話號碼。此代碼主要是做這項工作:Android光標錯誤 - 確保光標已正確初始化
// this query only return contacts with phone number and is not duplicated
phones = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
// we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",
null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
然後當它涉及到提取信息,並把它在每個單元:
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
我發現了一個錯誤,這是部分代碼:
04-07 10:43:46.489 17742-17760/com.example.chris.contactlistcustomlistview E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 134 rows, 34 columns.
04-07 10:43:46.489 17742-17760/com.example.chris.contactlistcustomlistview W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x416168e0)
04-07 10:43:46.489 17742-17760/com.example.chris.contactlistcustomlistview E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
因此,光標從Contacts.Contracts
跳躍到ContactsContract.CommonDataKinds.Phone
,它不喜歡它。但是,我還能如何獲得用戶的電話號碼?據我所知,它不在ContactsContracts.Contacts
。在進入CommonDataKinds
之前,我應該重新初始化光標還是其他東西?我該怎麼做?
感謝PlanetAstro,但是這並沒有解決它。同樣的錯誤。無論如何,我認爲movetonext和movetofirst總是從表中的第一個位置開始。 – CHarris