我已经设置了一个光标,我想要返回具有电话号码的联系人的列表视图。因此,在每个单元格中,我都会有联系人姓名和电话号码。此代码主要是做这项工作: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