问题
I've read similar topics and didn't find any answer that I could apply to my situation.
Long story short, I have an AutoCompleteTextView
with SimpleCursorAdapter
. It queries my SQLiteDatabase
for the matching drop down list of options.
Everything works fine, but when I press the "Home" button (onPause() -> onStop()
) if I've shortly before that used the AutoCompleteTextView
, upon re-entering the app (onRestart() -> onStart() -> onResume()
), I get this error:
03-05 19:17:42.186 13847-13847/com.ardovic.weatherappprototype E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ardovic.weatherappprototype, PID: 13847 java.lang.RuntimeException: Unable to resume activity {com.ardovic.weatherappprototype/com.ardovic.weatherappprototype.MainActivity}: java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@2cae0855 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4053) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4084) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1749) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6918) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@2cae0855 at android.app.Activity.performRestart(Activity.java:6660) at android.app.Activity.performResume(Activity.java:6688) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4042) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4084) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1749) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6918) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
It doesn't point to any of my code lines, so I suppose that it's the SimpleCursorAdapter problem. Here is the code for it (I've commented out unrelated parts, but they are still there):
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.dropdown_text,
null,
new String[]{CITY_COUNTRY_NAME},
new int[]{R.id.text});
actvCityCountryName.setAdapter(adapter);
actvCityCountryName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
cursor = (Cursor) listView.getItemAtPosition(position);
// cityCountryName = cursor.getString(cursor.getColumnIndexOrThrow(CITY_COUNTRY_NAME));
// actvCityCountryName.setText(cityCountryName);
// JSONWeatherTask task = new JSONWeatherTask();
// task.execute(new String[]{cityCountryName});
}
});
adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
@Override
public CharSequence convertToString(Cursor cursor) {
final int columnIndex = cursor.getColumnIndexOrThrow(CITY_COUNTRY_NAME);
final String cityCountryName = cursor.getString(columnIndex);
return (cityCountryName);
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
// cursor = getMatchingStates((constraint != null ? constraint.toString() : null));
return cursor;
}
});
I've already tried various suggestions, like calling stopManagingCursor(cursor)
, cursor.requery()
, actvCityCountryName.dismissDropDown()
, actvCityCountryName.clearListSelection()
, or actvCityCountryName.clearFocus()
in various places and lifecycle methods.
Looking forward for any suggestions that would solve this problem, thanx in advance!
来源:https://stackoverflow.com/questions/49115320/simplecursoradapter-issue-java-lang-illegalstateexception-trying-to-requery