我正在尝试通过启动打开联系人的意图来访问电话号码。 一切顺利,直到我尝试制作光标。我可以看到联系人的ID,但是在光线初始化的行上,应用程序停止工作。
代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri result = null ;
switch (requestCode) {
case 1001:
Bundle extras = data.getExtras();
Set<String> keys = extras.keySet();
Iterator<String> iterate = keys.iterator();
while (iterate.hasNext()) {
String key = iterate.next();
l(key + "[" + extras.get(key) + "]");
}
result = data.getData();
l( "Got a result: "+ result.toString());
break;
}
String id = result.getLastPathSegment();
l("key:" + id) ;
//Line with the problems , here the app stops
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, new String[]{id}, null);
if(cur.getCount() > 0) {
while (cur.moveToNext()) {
String idst = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
}
}
}
} else {
l( "A problem occured");
}
}
}
错误:
04-30 10:42:01.669: E/AndroidRuntime(2819): FATAL EXCEPTION: main
04-30 10:42:01.669: E/AndroidRuntime(2819): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/3034i335793838dba8dd3/2345 flg=0x1 (has extras) }} to activity {me.Messaging.planner/me.Messaging.planner.MyMessageActivity}: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0xc1c560
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.ActivityThread.deliverResults(ActivityThread.java:2918)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2970)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.ActivityThread.access$2000(ActivityThread.java:132)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1068)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.os.Looper.loop(Looper.java:150)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.ActivityThread.main(ActivityThread.java:4277)
04-30 10:42:01.669: E/AndroidRuntime(2819): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:42:01.669: E/AndroidRuntime(2819): at java.lang.reflect.Method.invoke(Method.java:507)
04-30 10:42:01.669: E/AndroidRuntime(2819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 10:42:01.669: E/AndroidRuntime(2819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 10:42:01.669: E/AndroidRuntime(2819): at dalvik.system.NativeStart.main(Native Method)
04-30 10:42:01.669: E/AndroidRuntime(2819): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0xc1c560
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:378)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.content.ContentProviderProxy.query(ContentProviderNative.java:414)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.content.ContentResolver.query(ContentResolver.java:264)
04-30 10:42:01.669: E/AndroidRuntime(2819): at me.Messaging.planner.MyMessageActivity.onActivityResult(MyMessageActivity.java:252)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.Activity.dispatchActivityResult(Activity.java:4108)
04-30 10:42:01.669: E/AndroidRuntime(2819): at android.app.ActivityThread.deliverResults(ActivityThread.java:2914)
04-30 10:42:01.669: E/AndroidRuntime(2819): ... 11 more
答案 0 :(得分:1)
只需使用
cur.moveToFirst()
在while循环之前。