SQLiteDatabaseHelper接收IllegalStateException:数据库未在构造函数中打开

时间:2013-06-17 03:07:35

标签: java android database sqlite

我清除了我的应用程序数据以测试创建新表格,但每当我从子类getWritableDatabase调用SQLiteDatabaseHelper时,我都会获得IllegalStateException: database not open

我不明白为什么我会让数据库无法打开'当我试图打开数据库时......

这是我创建的家庭活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    mPhotoArr = new ArrayList<File>();

    DBHelper dbHelp = new DBHelper(getApplicationContext());
}

我的DBHelper类的构造函数的开头

public DBHelper(Context context) {
    super(context, "MyDatabase", null, 1);
    db = getWritableDatabase(); //Crashing here
    ...

callstack是:

06-16 22:01:07.113: E/AndroidRuntime(24970): FATAL EXCEPTION: main
06-16 22:01:07.113: E/AndroidRuntime(24970): java.lang.RuntimeException: Unable to start activity ComponentInfo{**package**/**package**HomeActivity}: java.lang.IllegalStateException: database not open
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.os.Looper.loop(Looper.java:130)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at java.lang.reflect.Method.invokeNative(Native Method)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at java.lang.reflect.Method.invoke(Method.java:507)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at dalvik.system.NativeStart.main(Native Method)
06-16 22:01:07.113: E/AndroidRuntime(24970): Caused by: java.lang.IllegalStateException: database not open
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:555)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:137)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at **package**.DBHelper.<init>(DBHelper.java:101)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at **package**.HomeActivity.onCreate(HomeActivity.java:53)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 22:01:07.113: E/AndroidRuntime(24970):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-16 22:01:07.113: E/AndroidRuntime(24970):    ... 11 more

2 个答案:

答案 0 :(得分:1)

如果您不想使用自定义DataSource帮助程序类,那么我会像这样获取对数据库的引用:创建对扩展SQLiteOpenHelper的自定义类的引用,然后调用myReferenceToSQLiteOpenHelper.getWriteableDatabase()并将其存储在活动的SQLiteOpenHelper引用中。之后,您可以执行所需的任何操作。只需确保在完成后关闭数据库。

像这样:

MySQLiteOpenHelper mHelper = new MySQLiteOpenHelper(context);
SQLiteOpenHelper sHelper = mHelper.getWritableDatabase();

答案 1 :(得分:0)

添加我最终为其他可能发现此问题的人所做的事情。我为每个this答案创建了一个Application类的子类来保存我的数据库助手。每当我在需要查询我的数据库的活动中有活动或DAO时,就会使用Activity的子类创建活动,以便更容易地检索数据库。

请注意,使用ActionBar Sherlock时这确实有效 - 您只需要对相应的ABS活动进行子类化。