我要阅读 5- 联系人,所以我得到了这段代码并且顺利进行并且没有发生错误,但是当我添加这两行以保存名称和联系人以供以后使用时,出现了这个异常!
这是代码:
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// You know it has a number so now query it like this
Cursor phones1 = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
if (count > 5)
break;
while (phones1.moveToNext()) {
phoneNumber = phones1
.getString(phones1
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones1.close();
Toast.makeText(this,
contactId + " __ " + name + " # : " + phoneNumber,
Toast.LENGTH_SHORT).show();
Contact2[count] = name;
Numbers2[count] = phoneNumber;
count++;
}
这两个语句导致异常!
Contact2[count] = name;
Numbers2[count] = phoneNumber;
但是,没有这些语句,代码运行良好。
这是 logcat 窗口
06-15 18:00:02.745: I/ApplicationPackageManager(8639): cscCountry is not German : XEU
06-15 18:00:03.586: W/dalvikvm(8639): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
06-15 18:00:03.586: E/AndroidRuntime(8639): FATAL EXCEPTION: main
06-15 18:00:03.586: E/AndroidRuntime(8639): java.lang.IllegalStateException: Could not execute method of the activity
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.view.View$1.onClick(View.java:2154)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.view.View.performClick(View.java:2538)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.view.View$PerformClick.run(View.java:9152)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.os.Handler.handleCallback(Handler.java:587)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.os.Looper.loop(Looper.java:123)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-15 18:00:03.586: E/AndroidRuntime(8639): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 18:00:03.586: E/AndroidRuntime(8639): at java.lang.reflect.Method.invoke(Method.java:507)
06-15 18:00:03.586: E/AndroidRuntime(8639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-15 18:00:03.586: E/AndroidRuntime(8639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-15 18:00:03.586: E/AndroidRuntime(8639): at dalvik.system.NativeStart.main(Native Method)
06-15 18:00:03.586: E/AndroidRuntime(8639): Caused by: java.lang.reflect.InvocationTargetException
06-15 18:00:03.586: E/AndroidRuntime(8639): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 18:00:03.586: E/AndroidRuntime(8639): at java.lang.reflect.Method.invoke(Method.java:507)
06-15 18:00:03.586: E/AndroidRuntime(8639): at android.view.View$1.onClick(View.java:2149)
06-15 18:00:03.586: E/AndroidRuntime(8639): ... 11 more
06-15 18:00:03.586: E/AndroidRuntime(8639): Caused by: java.lang.NullPointerException
06-15 18:00:03.586: E/AndroidRuntime(8639): at oo.k.TestActivity.upload(TestActivity.java:156)
我只需要保存联系结果。