1
我正在使用以下代碼來使用SimpleCursorAdapter檢索聯繫人姓名和號碼。android使用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獲取聯繫人的電話號碼和名稱?
請幫我傢伙...
在此先感謝...