我知道有很多重复的问题,但我还没找到答案。
这是我的代码:
我有DatabaseHandler类,我有两个方法createTestTable(db);
和initializeTestTable(db);
public class DatabaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "AttendanceManagement";
private static DatabaseHandler sInstance;
public static synchronized DatabaseHandler getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHandler(context.getApplicationContext());
}
return sInstance;
}
private DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
createTestTable(db);
initializeTestTable(db);
}
private void createTestTable(SQLiteDatabase db) {
String CREATE_TEST_TABLE = "CREATE TABLE IF NOT EXISTS test" + "("
+ "name TEXT" + ")";
db.execSQL(CREATE_TEST_TABLE);
}
private void initializeTestTable(SQLiteDatabase db) {
// SQLiteDatabase db = this.getWritableDatabase();
// when I execute above code line I get following error.
// java.lang.IllegalStateException: getDatabase called recursively
ContentValues values = new ContentValues();
values.put("name", "test1");
// Inserting Row
db.insert("test", null, values);
db.close();
}
}
在我的活动中,我在onCreate()
db = DatabaseHandler.getInstance(this);
db.getWritableDatabase();
我的应用程序在第二行db.getWritableDatabase();
我正在关注崩溃日志
05-03 10:46:33.130: E/AndroidRuntime(5727): FATAL EXCEPTION: main
05-03 10:46:33.130: E/AndroidRuntime(5727): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cms.attendance/com.cms.attendance.fingerprintreader.newui.DashboardActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.cms.attendance/databases/AttendanceManagement
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.os.Looper.loop(Looper.java:137)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-03 10:46:33.130: E/AndroidRuntime(5727): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 10:46:33.130: E/AndroidRuntime(5727): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 10:46:33.130: E/AndroidRuntime(5727): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
05-03 10:46:33.130: E/AndroidRuntime(5727): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
05-03 10:46:33.130: E/AndroidRuntime(5727): at dalvik.system.NativeStart.main(Native Method)
05-03 10:46:33.130: E/AndroidRuntime(5727): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.cms.attendance/databases/AttendanceManagement
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-03 10:46:33.130: E/AndroidRuntime(5727): at com.cms.attendance.fingerprintreader.newui.DashboardActivity.onCreate(DashboardActivity.java:96)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.Activity.performCreate(Activity.java:5104)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-03 10:46:33.130: E/AndroidRuntime(5727): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-03 10:46:33.130: E/AndroidRuntime(5727): ... 11 more
请告诉我如何解决此问题。
答案 0 :(得分:0)
您可以尝试
public void onCreate(SQLiteDatabase db) {
createTestTable(db);
initializeTestTable(getInstance(context));
}
当你需要重新初始化数据库然后你可以关闭它。
答案 1 :(得分:0)
查看本教程http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
确保在进行任何修改后,您可以增加数据库的版本,以便重新创建数据库。
但是在onCreate方法中,您不应该关闭数据库,因为实现帮助程序的最佳实践中没有这样做。
答案 2 :(得分:0)
您可以在应用程序类中打开数据库
public class MyApplication extends Application {
private DatabaseHandler dbAdapter;
@Override
public void onCreate() {
dbAdapter = new DatabaseHandler (getApplicationContext());
dbAdapter.open();
}
在课堂上你可以通过以下代码获得
MyApplication myApplication = (MyApplication) this.getApplication();
DatabaseAdapter dbAdapter= myApplication.getDatabaseAdapter();