Android联系人数组

时间:2011-02-15 18:57:13

标签: android contacts

我想把手机上的手机联系人放到一个阵列上。我在模拟器中创建了4个联系人。

我在网上找到了这段代码,但是最后一行(263)让应用程序崩溃了:

String contactName = null;
ArrayList<String> myArr = new ArrayList<String>();
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while (cursor.moveToNext()) 
{
    contactName  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    myArr.add(contactName);         
}

wtxt1.setText(myArr.get(3));

wtxt1声明为TextView wtxt1;

代码有什么问题?

logcat的:

02-15 19:47:47.602: WARN/dalvikvm(586): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-15 19:47:47.602: ERROR/AndroidRuntime(586): FATAL EXCEPTION: main
02-15 19:47:47.602: ERROR/AndroidRuntime(586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bfarago.nevnap/com.bfarago.nevnap.MainActivity}: java.lang.NullPointerException
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.os.Looper.loop(Looper.java:123)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at java.lang.reflect.Method.invoke(Method.java:521)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at dalvik.system.NativeStart.main(Native Method)
02-15 19:47:47.602: ERROR/AndroidRuntime(586): Caused by: java.lang.NullPointerException
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at com.bfarago.nevnap.MainActivity.onCreate(MainActivity.java:263)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-15 19:47:47.602: ERROR/AndroidRuntime(586):     ... 11 more
02-15 19:47:48.852: WARN/ActivityManager(65):   Force finishing activity com.bfarago.nevnap/.MainActivity
02-15 19:47:49.361: WARN/ActivityManager(65): Activity pause timeout for HistoryRecord{43f939d0 com.bfarago.nevnap/.MainActivity}

调试:

Log.d("Checking size", String.valueOf(myArr.size()));
在while循环中

导致:

02-15 22:18:17.986: DEBUG/Checking size(452): 1
02-15 22:18:18.036: DEBUG/Checking size(452): 2
02-15 22:18:18.056: DEBUG/Checking size(452): 3
02-15 22:18:18.076: DEBUG/Checking size(452): 4

wtxt1.setText(String.valueOf(myArr.size()));

也会导致崩溃。

1 个答案:

答案 0 :(得分:1)

wtxt1为空 - 您需要为其指定值。

你可能有一个带有TextView的布局XML文件,所以你需要做类似的事情:

wtxt1 = (TextView) findViewById(R.id.your_text_view_id)