对字段槽 0,-1 的错误请求。 numRows = 164,numColumns = 63
Posted
技术标签:
【中文标题】对字段槽 0,-1 的错误请求。 numRows = 164,numColumns = 63【英文标题】:Bad request for field slot 0,-1. numRows = 164, numColumns = 63 【发布时间】:2014-03-04 20:39:51 【问题描述】:您好,我正在尝试通过 contentprovider 从联系人表中获取数据,但每次启动我的应用程序时它都会崩溃,
这是我的源代码:
public class WebpreneurActivity extends ListActivity
private DBHandler dbHelper;
private Cursor phones;
ImageButton imageButton;
public static long rowId;
@Override
protected void onCreate(Bundle savedInstanceState)
Log.d("database1" ,"0");
Log.d("Your Location4", "ok4:");
super.onCreate(savedInstanceState);
Log.d("database1" ,"1");
setContentView(R.layout.activity_webpreneur);
Log.d("database1" ,"2");
//dbHelper = new DBHandler(this);
Log.d("database1" ,"3");
// dbHelper.open();
phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.Contacts._ID, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
if (phones.getCount() > 0)
while (phones.moveToFirst())
if (Integer.parseInt(phones.getString(phones
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactId = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
System.out.println("contactId="+ contactId+ ", name=" + name + ", phoneNumber=" + phoneNumber);
phones.close();
而我的 Logcat 是:
02-07 17:00:54.905: D/database1(19505): 0
02-07 17:00:54.905: D/Your Location4(19505): ok4:
02-07 17:00:54.905: D/database1(19505): 1
02-07 17:00:54.985: D/database1(19505): 2
02-07 17:00:54.985: D/database1(19505): 3
02-07 17:00:55.045: E/CursorWindow(19505): Bad request for field slot 0,-1. numRows = 164, numColumns = 63
02-07 17:00:55.045: D/androidRuntime(19505): Shutting down VM
02-07 17:00:55.045: W/dalvikvm(19505): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
02-07 17:00:55.085: E/AndroidRuntime(19505): FATAL EXCEPTION: main
02-07 17:00:55.085: E/AndroidRuntime(19505): java.lang.RuntimeException: Unable to start activity ComponentInfocom.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.os.Looper.loop(Looper.java:138)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.ActivityThread.main(ActivityThread.java:3701)
02-07 17:00:55.085: E/AndroidRuntime(19505): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 17:00:55.085: E/AndroidRuntime(19505): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 17:00:55.085: E/AndroidRuntime(19505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-07 17:00:55.085: E/AndroidRuntime(19505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-07 17:00:55.085: E/AndroidRuntime(19505): at dalvik.system.NativeStart.main(Native Method)
02-07 17:00:55.085: E/AndroidRuntime(19505): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.database.CursorWindow.getString_native(Native Method)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.database.CursorWindow.getString(CursorWindow.java:329)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
02-07 17:00:55.085: E/AndroidRuntime(19505): at com.webpreneur_contactlist.WebpreneurActivity.onCreate(WebpreneurActivity.java:58)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-07 17:00:55.085: E/AndroidRuntime(19505): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
02-07 17:00:55.085: E/AndroidRuntime(19505): ... 11 more
02-07 17:04:53.027: I/Process(19505): Sending signal. PID: 19505 SIG: 9
【问题讨论】:
为什么要遍历返回的光标? 我尝试了不同的代码从联系人表中获取数据,这是我从堆栈溢出中复制的代码之一,因为我自己的代码无法获得结果,请您帮我解决这个问题解决,因为我陷入了困境。 再次:为什么要遍历返回的光标? 您的意思是 (phones.getCount() > 0) 先生吗?实际上我刚开始在 android 中编程,我没有足够的想法,请帮助我并告诉我我在哪里制作的错误。 不,我在问为什么你会遍历返回的光标吗? 【参考方案1】:只是不要将ContactsContract.Contacts._ID
传递给query()
请求:
phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
更新: 实际上,您的代码中还有一些问题
ContactsContract.CommonDataKinds.Phone
表中没有 ContactsContract.Contacts.HAS_PHONE_NUMBER
列。您应该只取ContactsContract.CommonDataKinds.Phone.NUMBER
并检查它是否为空。
您还应该使用while (phones.moveToNext())
而不是while (phones.moveToFirst())
请试试这个代码:
phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
if (phones.getCount() > 0)
while (phones.moveToNext())
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (!TextUtils.isEmpty(phoneNumber))
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactId = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
System.out.println("contactId="+ contactId+ ", name=" + name + ", phoneNumber=" + phoneNumber);
【讨论】:
好吧,你真的很摇滚,先生,你真的很棒,非常感谢你,非常感谢......以上是关于对字段槽 0,-1 的错误请求。 numRows = 164,numColumns = 63的主要内容,如果未能解决你的问题,请参考以下文章