Android 联系人数组

发布于 2024-10-17 16:15:27 字数 3596 浏览 2 评论 0原文

我想将手机上的电话联系人放入数组中。我已在模拟器中创建了 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()));

还导致崩溃。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-10-24 16:15:27

wtxt1 为空——您需要为其分配一个值。

您可能有一个包含 TextView 的布局 XML 文件,因此您需要执行以下操作:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文