java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:SQLiteDatabase:/data/data/com.testapp.test420/databases/bazeQuiz

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.testapp.test420/databases/bazeQuiz

我试图通过在 mainactivity 中按下一个按钮来打开另一个 activity,但是按下按钮时程序崩溃了,我不知道为什么。

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shitapps.howtolosedignity/com.shitapps.howtolosedignity.ActivityQuiz}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.shitapps.howtolosedignity/databases/masterblaster
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.shitapps.howtolosedignity/databases/masterblaster
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.shitapps.howtolosedignity.DbHelper.getAllQuestions(DbHelper.java:104)
at com.shitapps.howtolosedignity.ActivityQuiz.onCreate(ActivityQuiz.java:32)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
at android.app.ActivityThread.access0(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        saakt = (Button)findViewById(R.id.button2);

        saakt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }

            ;
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

我使用 Android Studio。

编辑:

public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase=this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Question quest = new Question();
            quest.setId(cursor.getInt(0));
            quest.setJautajums(cursor.getString(1));
            quest.setAtbilde(cursor.getString(2));
            quest.setOpcijaA(cursor.getString(3));
            quest.setOpcijaB(cursor.getString(4));
            quest.setOpcijaC(cursor.getString(5));
            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    return quesList;
}

dbase已经关闭,无法使用。尝试使用本地实例。

改变

dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

您不应在每次查询后关闭数据库。您可以将其作为单例引用。您可以通过参考资料找到更多信息:

Android SQLite DB When to Close

Best place to close database connection