我正在使用以下代码使用 SimpleCursorAdapter 检索联系人姓名和号码。
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
// boolean mShowInvisible = false;
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP
+ " = '1'";
String[] selectionArgs = null;
final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
m_curContacts = managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
String[] fields = new String[] { ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
m_slvAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, m_curContacts, fields,
new int[] { android.R.id.text1,android.R.id.text2 });
但是当我运行它时,我收到以下错误
04-07 17:05:34.980: WARN/dalvikvm(614): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-07 17:05:34.990: ERROR/AndroidRuntime(614): Uncaught handler: thread main exiting due to uncaught exception
04-07 17:05:35.080: ERROR/AndroidRuntime(614): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kpj4s.contactadapter/com.kpj4s.contactadapter.ContactAdapter}: **java.lang.IllegalArgumentException: column 'data1' does not exist**
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.os.Looper.loop(Looper.java:123)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at dalvik.system.NativeStart.main(Native Method)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:339)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:91)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.kpj4s.contactadapter.ContactAdapter.ReadContacts(ContactAdapter.java:158)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at com.kpj4s.contactadapter.ContactAdapter.onCreate(ContactAdapter.java:132)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-07 17:05:35.080: ERROR/AndroidRuntime(614): ... 11 more
当我删除ContactsContract.CommonDataKinds.Phone.NUMBER时,一切正常。当我使用那段代码时,错误就会出现。
有没有办法使用 SimpleCursorAdapter 获得联系人电话号码和姓名?
请帮帮我...
提前致谢 ...