开发者

Android contacts array

I would like to put the phone contacts on my phone to an Array. I've created 4 contacts in the Emulator.

I have found this code on the web, but the last row (263) makes the app crash:

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 is declared as TextView wtxt1;

What is wrong with the code?

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}

Debug:

Log.d("Checking size", String.valueOf(myArr.size()));

in the while cycle results in:

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

And

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

also results in crash.


wtxt1 is null -- you need to assign a value to it.

You probably have a layout XML file with a TextView in it, so you'd need to do something like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜