SQLite:尝试重新打开已经关闭的对象

时间:2015-04-18 17:45:27

标签: java android sqlite

我有导航抽屉,当我选择第二个项目时,有时会看到一些关于尝试重新打开已经关闭的对象的错误:SQLiteDatabase

我不知道如何解决这个问题

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final String[] fragments ={"حذف تمامی پیام ها","ذخیره تمامی پیام ها"};
    mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.main_drawer);
    mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(),android.R.layout.simple_expandable_list_item_1, fragments));
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position)
            {
                case 0:
                {
                    final SQLiteDatabase mydatabase = openOrCreateDatabase("CopyCollection", MODE_PRIVATE, null);
                    mydatabase.execSQL("DELETE FROM Details;");
                    mydatabase.close();
                    myBrowser.loadUrl("javascript:fill_comment()");
                    break;
                }
                case 1:
                {

                    final SQLiteDatabase mydatabase1 = openOrCreateDatabase("CopyCollection", MODE_PRIVATE, null);
                    Cursor crs = mydatabase1.rawQuery("SELECT * FROM Groups;",null);
                    String[] array = new String[crs.getCount()];
                    arrayID = new String[crs.getCount()];
                    Count = crs.getCount();
                    int i = 0;
                    while(crs.moveToNext()){
                        String uname = crs.getString(crs.getColumnIndex("GroupName"));
                        String num = crs.getString(crs.getColumnIndex("ID"));
                        arrayID[i] = num;
                        array[i] = uname;
                        i++;
                    }

                    final AlertDialog.Builder builder = new AlertDialog.Builder(AndroidHTMLActivity.this);
                    builder.setTitle("گروه خود را انتخاب کنید");
                    builder.setItems(array, new DialogInterface.OnClickListener()
                    {
                        public void onClick(final DialogInterface dialog, final int which)
                        {
                            if(which == 0)
                            {
                                if(Count == 4)
                                {
                                    Toast.makeText(getApplicationContext(),"نسخه رایگان",Toast.LENGTH_SHORT).show();
                                }
                                else if (Count < 4)
                                {
                                    myBrowser.loadUrl("javascript:getCheckedBox()");
                                    AlertDialog.Builder alert = new AlertDialog.Builder(AndroidHTMLActivity.this);
                                    alert.setTitle("گروه جدید");
                                    alert.setMessage("نام جدید را وارد کنید");
                                    final EditText input = new EditText(AndroidHTMLActivity.this);
                                    input.setInputType(1);
                                    alert.setView(input);
                                    alert.setPositiveButton("تایید", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                            m_Text = input.getText().toString();
                                            mydatabase1.execSQL("INSERT INTO Groups (GroupName) VALUES('" + m_Text + "');");
                                            Cursor c3 = mydatabase1.rawQuery("SELECT * FROM Groups ORDER BY ID DESC LIMIT 1;", null);
                                            while(c3.moveToNext()){
                                                int ID = c3.getInt(0);
                                                LastDir = String.valueOf(ID);
                                            }
                                            for(int i=0; i<ArraySize; i++)
                                            {
                                                Cursor CDetails = mydatabase1.rawQuery("SELECT * FROM Details WHERE ID = "+CheckBoxes[i]+" ;", null);
                                                if (CDetails.moveToFirst()) {
                                                    appName = CDetails.getString(1);
                                                    txtClip = CDetails.getString(2);
                                                    text_Date = CDetails.getString(3);
                                                }
                                                CDetails.close();
                                                if(i < 4)
                                                {
                                                    mydatabase1.execSQL("INSERT INTO MainContent(AppName,Txt,GroupID,Time)VALUES('" + appName + "','" + txtClip + "','" + LastDir + "','" + text_Date + "');");
                                                    mydatabase1.execSQL("DELETE FROM Details WHERE ID = " + CheckBoxes[i] + ";");
                                                    mydatabase1.close();
                                                }
                                                else
                                                {
                                                    Toast.makeText(getApplicationContext(),"نسخه رایگان",Toast.LENGTH_SHORT).show();
                                                    break;
                                                }
                                            }
                                        }
                                    });
                                    alert.setNegativeButton("انصراف", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                            myBrowser.post(new Runnable()
                                            {
                                                public void run()
                                                {
                                                    myBrowser.loadUrl("javascript:fill_comment()");
                                                }
                                            });
                                        }
                                    });
                                    alert.show();
                                }
                            }
                            else if(which > 0)
                            {

                            }
                        }
                    });
                    builder.show();
                    mydatabase1.close();
                }
            }
        }
    });
 }

1 个答案:

答案 0 :(得分:1)

case 1:

final SQLiteDatabase mydatabase1 = openOrCreateDatabase("CopyCollection", MODE_PRIVATE, null);

case 1代码块的末尾关闭。因此,当你点击对话框按钮时,你正在做 -

mydatabase1.execSQL("INSERT INTO Groups (GroupName) VALUES('" + m_Text + "');");

这里你得到了例外。 mydatabase1已经关闭。你现在应该在onClick()内获得一个新的参考。