電話の連絡先を取得して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;
}
}