我正在嘗試通過AsyncTask中的聯繫人簿中的號碼查找聯繫人姓名。有時我會遇到以下錯誤,我認爲這會導致應用程序崩潰。請任何幫助。應用程序未關閉遊標或數據庫對象android
logcat的
08-05 09:00:49.627 24988-27371/com.fake.fake E/CursorLeakDetecter: PossibleCursorLeak:content://com.android.contacts/phone_lookup/%2B923310433708,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:323)
at com.fake.fake.GV.getContactName(GV.java:177)
at com.fake.fake.MySQLiteHelper.GetNumbers(MySQLiteHelper.java:361)
at com.fake.fake.MainActivity$taskLoadContacts.doInBackground(MainActivity.java:930)
at com.fake.fake.MainActivity$taskLoadContacts.doInBackground(MainActivity.java:921)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
從數據庫中檢索聯繫人號碼。
public ArrayList<MyMessageDetails> GetNumbers(String query) {
ArrayList<MyMessageDetails> numbers = new ArrayList<MyMessageDetails>();
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
do {
MyMessageDetails item = new MyMessageDetails();
item.setNumber(cursor.getString(0));
String name = GV.getContactName(item.getNumber());
if (name == null) {
name = item.getNumber();
}
item.setName(name);
item.setTime(cursor.getString(3));
numbers.add(item);
} while (cursor.moveToNext());
}
return numbers;
}
檢索聯繫人姓名。
public static String getContactName(String phoneNumber) {
if (phoneNumber != null) {
ContentResolver cr = ApplicationContextProvider.getContext().getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return "";
}
String contactName = null;
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
return phoneNumber;
}
請參閱:https://stackoverflow.com/questions/ 4991179/application-did-not-close-the-cursor-or-database-object-that-opened-here-rq = 1 –
感謝您的鏈接,但錯誤來自「getContactName」而不是來自GetNumbers。 – user934820