SimpleCursorAdapter issue - “java.lang.IllegalStateException: trying to requery an already closed cursor”

怎甘沉沦 提交于 2019-12-11 16:09:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!