I know there are lot of duplicate questions asked but I haven't found my answer yet.
Here is my code: I have DatabaseHandler class where I have two methods createTestTable(db);
and 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();
}
}
Inside my activity I have following code in onCreate()
db = DatabaseHandler.getInstance(this);
db.getWritableDatabase();
My application crashes after second line db.getWritableDatabase();
I am getting following crash log
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
Please let me know how can I solve this issue.
Aucun commentaire:
Enregistrer un commentaire