我在我的main.xml文件中添加了EditText,Button和ListView。用户将输入全名或一些名称字母,然后单击按钮。按钮单击事件将显示与用户查询对应的联系人列表中的所有姓名和电话号码。
这是我的onClick函数代码:
Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, editText.getText().toString());
Cursor idCursor = getContentResolver().query(lookupUri, null, null, null, null);
startManagingCursor(idCursor);
String wherestmt = ContactsContract.Data.CONTACT_ID + " = " + idCursor + " And " + ContactsContract.Data.MIMETYPE + " = " + ContactsContract.CommonDataKinds.Phone.CONTENT_URI + "'";
Cursor datacursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,null,wherestmt,null,null);
int nameIdx = datacursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME);
int phoneIdx = datacursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
datacursor.moveToFirst();
list = new ArrayList<String>();
do{
String name = datacursor.getString(nameIdx);
String phone = datacursor.getString(phoneIdx);
list.add(name + " " + phone);
}while(datacursor.moveToNext());
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);
listView.setAdapter(adapter);
但是,我在以下一行得到例外:
Cursor datacursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,null,wherestmt,null,null);
例外情况如下:
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
FATAL EXCEPTION: main E/AndroidRuntime(1287): android.database.sqlite.SQLiteException: near "@412c63d0": syntax error: ,content.ContentResolver$CursorWrapperInner@412c63d0 And mimetype = 'content://com.android.contacts/data/phones')) 04-01 13:48:46.616: E/AndroidRuntime(1287): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179) 04-01 13:48:46.616: E/AndroidRuntime(1287): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
答案 0 :(得分:0)
可能是您出错的地方,您忘记'
之前的ContactsContract.CommonDataKinds.Phone.CONTENT_URI
:
String wherestmt = ContactsContract.Data.CONTACT_ID + " = " + idCursor + " And " + ContactsContract.Data.MIMETYPE + " = " + ContactsContract.CommonDataKinds.Phone.CONTENT_URI + "'";
更改为:
String wherestmt = ContactsContract.Data.CONTACT_ID + " = " + idCursor + " And " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_URI + "'";
答案 1 :(得分:0)
您在NULL
检查editText.getText().toString());
或清空{{1}}。我认为这就是问题所在。