我希望从手机中获取所有联系人并将其显示在列表中>因为我已经编写了一个工作正常的代码。但有时我得到空指针异常,因为我的应用程序崩溃。我不我知道这个问题是什么时候它可以完美地工作但有时会给出空指针异常。
代码
public static JSONArray getAllContactList(Context context) {
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
alAllContacts = new ArrayList<ContactModel>();
while (!(c == null) && c.moveToNext()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (number.equalsIgnoreCase("1")) {
// Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
// ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = '" + id + "'", null, null);
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext() && phones != null) {
String contactName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactNumber = contactNumber.replace("+", "");
if (contactNumber.length() > 10) {
contactNumber = contactNumber.substring(2);
}
// contactNumber.replace("+91", "");
alAllContacts.add(new ContactModel(contactName, contactNumber));
//
}
}
jsonArray = new JSONArray();
for (int i = 0; i < alAllContacts.size(); i++) {
jsonArray.put(alAllContacts.get(i).getJSONObject());
}
}
c.close();
return jsonArray;
}
如果我得到Null指针,则logcat显示它在此行
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext() && phones != null) {
logcat的
E/CursorLeakDetecter﹕ PossibleCursorLeak:content://com.android.contacts/data/phones,QueryCounter:5
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.content.ContentResolver.query(ContentResolver.java:399)
at android.content.ContentResolver.query(ContentResolver.java:316)
at example.com.pocketdocs.CommonFunctions.CommonFunctions.getAllContactList(CommonFunctions.java:68)
at example.com.pocketdocs.Group.ContactListFragment$getContactsyncedContacts.doInBackground(ContactListFragment.java:250)
at example.com.pocketdocs.Group.ContactListFragment$getContactsyncedContacts.doInBackground(ContactListFragment.java:246)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
09-05 13:31:03.774 28948-28977/example.com.pocketdocs E/CursorLeakDetecter﹕ PossibleCursorLeak:content://com.android.contacts/data/phones,QueryCounter:6
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.content.ContentResolver.query(ContentResolver.java:399)
at android.content.ContentResolver.query(ContentResolver.java:316)
at example.com.pocketdocs.CommonFunctions.CommonFunctions.getAllContactList(CommonFunctions.java:68)
at example.com.pocketdocs.Group.ContactListFragment$getContactsyncedContacts.doInBackground(ContactListFragment.java:250)
at example.com.pocketdocs.Group.ContactListFragment$getContactsyncedContacts.doInBackground(ContactListFragment.java:246)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
答案 0 :(得分:0)
你错过了单引号(因为id是字符串),所以改变
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
到
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " ='" + id + "'", null, null);