

はじめまして。アプリ開発初心者で、行き詰ってしまったので質問させていただきます。

### 実現したい機能

前画面から渡されたテキストをを基に、データベースに保存されているデータをテキストビューに表示する。

**エラーメッセージ**

[code]

    04-08 21:53:17.690 17364-17364/com.example.android.battle_poke E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalStateException: database /data/data/com.example.android.battle_poke/databases/PokeParty.db (conn# 0) already closed
            at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2085)
            at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:413)
            at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:400)
            at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
            at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
            at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:147)
            at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:178)
            at android.widget.CursorAdapter.getItemId(CursorAdapter.java:219)
            at android.widget.AbsListView.onSaveInstanceState(AbsListView.java:1657)
            at android.view.View.dispatchSaveInstanceState(View.java:9886)
            at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2310)
            at android.widget.AdapterView.dispatchSaveInstanceState(AdapterView.java:770)
            at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2296)
            at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2296)
            at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2296)
            at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2296)
            at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2296)
            at android.view.View.saveHierarchyState(View.java:9869)
            at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1578)
            at android.app.Activity.onSaveInstanceState(Activity.java:1163)
            at android.support.v4.app.SupportActivity.onSaveInstanceState(SupportActivity.java:74)
            at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:564)
            at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:509)
            at android.app.Activity.performSaveInstanceState(Activity.java:1113)
            at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185)
            at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2844)
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2902)
            at android.app.ActivityThread.access$1000(ActivityThread.java:129)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4475)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
            at dalvik.system.NativeStart.main(Native Method)
    
[/code]

**コード**

[code]

    public class PartyActivity extends AppCompatActivity {
    
    
    private TextView title;
    private TextView name1;
    private TextView detail1;
    private TextView name2;
    private TextView detail2;
    private TextView name3;
    private TextView detail3;
    private TextView name4;
    private TextView detail4;
    private TextView name5;
    private TextView detail5;
    private TextView name6;
    private TextView detail6;
    private TextView party_memo;
    private TextView battle_memo;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_party);
    
        title=(TextView)findViewById(R.id.partyTitle_textView);
        name1=(TextView)findViewById(R.id.pokemon1_name_textView);
        detail1=(TextView)findViewById(R.id.pokemon1_detail_textView);
        name2=(TextView)findViewById(R.id.pokemon2_name_textView);
        detail2=(TextView)findViewById(R.id.pokemon2_detail_textView);
        name3=(TextView)findViewById(R.id.pokemon3_name_textView);
        detail3=(TextView)findViewById(R.id.pokemon3_detail_textView);
        name4=(TextView)findViewById(R.id.pokemon4_name_textView);
        detail4=(TextView)findViewById(R.id.pokemon4_detail_textView);
        name5=(TextView)findViewById(R.id.pokemon5_name_textView);
        detail5=(TextView)findViewById(R.id.pokemon5_detail_textView);
        name6=(TextView)findViewById(R.id.pokemon6_name_textView);
        detail6=(TextView)findViewById(R.id.pokemon6_detail_textView);
        party_memo=(TextView)findViewById(R.id.party_memo_textView);
        battle_memo=(TextView)findViewById(R.id.battle_memo_textView);
    
        Intent intent = getIntent();
        String str = intent.getStringExtra("party_title");
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    
        //Cursorで特定のタプルの値を取得するための準備
        String[] columns = new String[]{COLUMN_NAME_POKE1, COLUMN_DETAIL_POKE1,
                                        COLUMN_NAME_POKE2, COLUMN_DETAIL_POKE2,
                                        COLUMN_NAME_POKE3, COLUMN_DETAIL_POKE3,
                                        COLUMN_NAME_POKE4, COLUMN_DETAIL_POKE4,
                                        COLUMN_NAME_POKE5, COLUMN_DETAIL_POKE5,
                                        COLUMN_NAME_POKE6, COLUMN_DETAIL_POKE6,
                                        COLUMN_NAME_TITLE,COLUMN_PARTY_MEMO,COLUMN_BATTLE_MEMO};
        String where = COLUMN_NAME_TITLE + " = ?";
    
    
        PokeDbHelper mDbhelper = new PokeDbHelper(this);
        SQLiteDatabase db = mDbhelper.getReadableDatabase();
    
    
        //Cursorでデータベースから「ListViewより取得したパーティ名と一致するタプルの値」を得る
        Cursor c =db.query(TABLE_NAME,
                columns,
                where,
                new String[]{str},
                null,
                null,
                null);
    
        //TextViewに表示
        try {
            c.moveToFirst();
    
            name1.setText(c.getString(0));
            detail1.setText(c.getString(1));
            name2.setText(c.getString(2));
            detail2.setText(c.getString(3));
            name3.setText(c.getString(4));
            detail3.setText(c.getString(5));
            name4.setText(c.getString(6));
            detail4.setText(c.getString(7));
            name5.setText(c.getString(8));
            detail5.setText(c.getString(9));
            name6.setText(c.getString(10));
            detail6.setText(c.getString(11));
            title.setText(c.getString(12));
            party_memo.setText(c.getString(13));
            battle_memo.setText(c.getString(14));
        }catch (Exception e){
            Log.e("TAG","message",e);
        } finally {
            if(db != null && db.isOpen())
            mDbhelper.close();
        }
    
[/code]

英語版のStack
Overflowの似たような質問を見て、dbが開いているかどうかチェックしてクローズしてみたりしたのですが、何分初心者なもので、どのような現象が起きているのかすらはっきり理解できておりません。

回答よろしくお願いいたします。情報不足等あれば注意喚起していただけると嬉しいです。


CC BY-SA 4.0
2018-04-08T13:10:31.713
43063
2021-03-07T04:40:00.557
2021-03-07T04:40:00.557
3060
27149
question
android sqlite
Androidアプリ開発　エラー「db already closed」