人們說,它可能是因爲當你不關閉遊標或數據庫時造成的。但我有一些奇怪的東西。 我嘗試在OK按鈕上打開數據庫,在應用程序開始的MainActivity中的對話框片段窗口(resultClass)中單擊。SQLite數據庫鎖定在DialogFragment onClick事件中
,在我的DialogFragment類
public Score_DS dsoop=new Score_DS(getActivity());
..
case R.id.button1:
dsoop.open();// Here comes the crash
...
dsoop.close();
Score_DS調用DialogFragment窗口
@SuppressLint("NewApi")
public void vasya(View v){
DialogFragment dlg_rec=new resultClass();
dlg_rec.show(getFragmentManager(), "dlg_rec");}
的方法是我的DataSource類與數據庫進行操作。
public class Score_DS {
ContentValues cv= new ContentValues();
DB dbHelper;
SQLiteDatabase db;
public Score_DS(Context context) {
dbHelper = new DB(context);
}
public void open() throws SQLException {
if (dbHelper != null) {
db = dbHelper.getWritableDatabase();
}
}
...
(here I have a couple of methods that I've never called yet)
}
DB.java是這個
package com.wordpress.jimmaru.tutorial.SimpleGame;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DB extends SQLiteOpenHelper implements BaseColumns{
private static final String DATABASE_NAME = "mygame.db";
private static final int DATABASE_VERSION = 1;
private static DB sInstance;
ContentValues cv;
public DB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Records(_id INTEGER PRIMARY KEY, Player_Name VARCHAR(50), Player_Score VARCHAR(50));");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
// TODO Auto-generated method stub
}
}
所以,再次。我通過點擊一個按鈕,在應用程序的一開始就調用DialogFragment窗口。當我點擊我的dialogfragment窗口中的OK按鈕時,應用程序崩潰並顯示數據庫已被鎖定。我關閉了所有東西(db.close)。我沒有打開遊標。在MainActivity中我有另一個數據庫相關的方法。
Score_DS dseed=new Score_DS(this);
...
public void clearall(View v)
{
dseed.open();
dseed.execution("DELETE FROM Records");
dseed.close();
}
它工作得很好。 什麼可能是錯的?