-1

我想获取电话联系人并显示在 listView 中。但我无法在数组列表中加载字符串值。我不知道如何解决这个问题。

记录猫错误:

01-07 12:43:49.381: E/AndroidRuntime(863): FATAL EXCEPTION: main
01-07 12:43:49.381: E/AndroidRuntime(863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rathnasoft.sample/com.rathnasoft.sample.ContactsPhone}: java.lang.NullPointerException
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.os.Looper.loop(Looper.java:123)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-07 12:43:49.381: E/AndroidRuntime(863):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 12:43:49.381: E/AndroidRuntime(863):  at java.lang.reflect.Method.invoke(Method.java:521)
01-07 12:43:49.381: E/AndroidRuntime(863):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 12:43:49.381: E/AndroidRuntime(863):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-07 12:43:49.381: E/AndroidRuntime(863):  at dalvik.system.NativeStart.main(Native Method)
01-07 12:43:49.381: E/AndroidRuntime(863): Caused by: java.lang.NullPointerException
01-07 12:43:49.381: E/AndroidRuntime(863):  at com.rathnasoft.sample.ContactsPhone.displayContacts(ContactsPhone.java:67)
01-07 12:43:49.381: E/AndroidRuntime(863):  at com.rathnasoft.sample.ContactsPhone.onCreate(ContactsPhone.java:22)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 12:43:49.381: E/AndroidRuntime(863):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-07 12:43:49.381: E/AndroidRuntime(863):  ... 11 more

编码部分:

public class ContactsPhone extends Activity {
private ListView lvContact;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts);
    lvContact = (ListView) findViewById(R.id.listView1);
    ArrayList<String> list = displayContacts();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,list);
    lvContact.setAdapter(adapter);


}

private ArrayList<String> displayContacts() {

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);

    ArrayList<String> contact = null;
    String phoneNo = null;
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            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()) {
                    phoneNo = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    System.out.println("name" + name + "ph no" + phoneNo);
                    Toast.makeText(this,
                            "Name: " + name + ", Phone No: " + phoneNo,
                            Toast.LENGTH_SHORT).show();
                }


                pCur.close();
            }
            contact.add(name + "\n" + phoneNo);
        }
    }
    return contact;

}
}
4

2 回答 2

3

我认为你的问题是没有初始化 arrayList

    ArrayList<String> contact = null;

改成

    ArrayList<String> contact = new ArrayList<String>();

因此零点

编辑 - 哈哈被打败了:)

于 2013-01-07T07:37:16.707 回答
2

创建数组列表而不是将其设置为 null:

ArrayList<String> contact =  new ArrayList<String>();
于 2013-01-07T07:36:36.070 回答