我試着去閱讀一個聯繫人的電話號碼,但我不能得到它的工作...Android應用程序的問題讀取電話號碼
我的代碼是完全一樣的here
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
if (cur.getCount() > 0)
{
contacts = new String[cur.getCount()];
numbers = new String[cur.getCount()];
int index = 0;
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts[index] = name;
MyListAdapter listAdapt = new MyListAdapter(MeetUp.this, R.layout.row, contacts);
MeetUp.this.setListAdapter(listAdapt);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while(pCur.moveToNext());
{
numbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
index++;
}
}
我logcat的輸出:
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): java.lang.RuntimeException: Unable to start activity ComponentInfo{michaels.meeting/michaels.meeting.MeetUp}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.os.Looper.loop(Looper.java:144)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at java.lang.reflect.Method.invoke(Method.java:521)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at dalvik.system.NativeStart.main(Native Method)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.database.CursorWrapper.getString(CursorWrapper.java:140)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at michaels.meeting.MeetUp.onCreate(MeetUp.java:47)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): ... 11 more
它未能肯定有一些,但d後的接觸ebugging我認爲pCur沒有數字列。任何想法爲什麼會發生這種情況?在此先感謝 - 麥克
K Nope!這沒有奏效,但無論如何感謝! – mixkat 2011-03-25 17:14:45
代碼中的哪一行導致異常被拋出? – 2011-03-25 17:28:02
K這一個:numbers [index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); – mixkat 2011-03-25 17:29:55