Android 光标错误 - 确保光标已正确初始化
Android Cursor error - Make sure cursor is initialised correctly
我已经设置了一个游标,我想 return 具有 phone 号码的联系人列表视图。因此,在每个单元格中,我将在下方显示联系人姓名和 phone 号码。此代码主要完成工作:
// 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.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.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
并且它不喜欢它。但是我还能如何获得用户的 phone 号码呢?据我所知,它不在 ContactsContracts.Contacts
中。我应该在转到 CommonDataKinds
之前重新初始化光标或其他东西吗?我该怎么做?
您是否尝试过在 phones.moveToNext() 之前调用 phones.moveToFirst()?它移动到第一行。这是因为您当前的查询 returns 一个位于第一个条目之前的 Cursor 对象。
你可以试试写:
if (phones.moveToFirst()) {
while (phones.moveToNext()) {
// etc
if (phones != null) {
phones.moveToFirst();
}
我已经设置了一个游标,我想 return 具有 phone 号码的联系人列表视图。因此,在每个单元格中,我将在下方显示联系人姓名和 phone 号码。此代码主要完成工作:
// 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.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.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
并且它不喜欢它。但是我还能如何获得用户的 phone 号码呢?据我所知,它不在 ContactsContracts.Contacts
中。我应该在转到 CommonDataKinds
之前重新初始化光标或其他东西吗?我该怎么做?
您是否尝试过在 phones.moveToNext() 之前调用 phones.moveToFirst()?它移动到第一行。这是因为您当前的查询 returns 一个位于第一个条目之前的 Cursor 对象。
你可以试试写:
if (phones.moveToFirst()) {
while (phones.moveToNext()) {
// etc
if (phones != null) {
phones.moveToFirst();
}