我是相當新的Android,並試圖創建一個數據庫,其中包含一個表來存儲電子郵件地址,用戶名和密碼。將破發點出現下面的代碼行的程序之後被扔了問題:Android SQLite錯誤導致getWriteableDatabase()
SQLiteDatabase database = this.getWriteableDatabase();
下面是圍繞上面的一行代碼:
// Create A User
public boolean createUser(String userEmail, String userName, String userPassword) {
ContentValues values = new ContentValues();
values.put(KEY_USER_EMAIL, userEmail);
values.put(KEY_USER_NAME, userName);
values.put(KEY_USER_PASSWORD, userPassword);
SQLiteDatabase database = this.getWritableDatabase();
database.close();
}
當試圖創建一個用戶我得到以下錯誤:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3052)
at android.view.View.performClick(View.java:3519)
at android.view.View$PerformClick.run(View.java:14140)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3047)
at android.view.View.performClick(View.java:3519)
at android.view.View$PerformClick.run(View.java:14140)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: database /data/data/com.pastpapercreator.chemistrypastpapercreator/databases/chemistryPastPaperCreator (conn# 0) already closed
at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2088)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:690)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:176)
at com.pastpapercreator.chemistrypastpapercreator.helper.DatabaseHelper.createUser(DatabaseHelper.java:111)
at com.pastpapercreator.chemistrypastpapercreator.CreateAccount.createAccount(CreateAccount.java:83)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3047)
at android.view.View.performClick(View.java:3519)
at android.view.View$PerformClick.run(View.java:14140)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
任何幫助,非常感謝。
下面是用於創建數據庫的代碼:
// User Table Column Names
private static final String KEY_USER_ID = "uId";
private static final String KEY_USER_EMAIL = "uEmail";
private static final String KEY_USER_NAME = "uName";
private static final String KEY_USER_PASSWORD = "uPassword";
// User Table Creation Statement
private static final String CREATE_TABLE_USER = "CREATE TABLE " + TABLE_USER +
"(" + KEY_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USER_EMAIL + " TEXT," + KEY_USER_NAME + " TEXT," + KEY_USER_PASSWORD + " TEXT" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
// Create Required Tables
database.execSQL(CREATE_TABLE_USER);
database.close();
}
你可能有一個錯誤在你的生命週期回調,如數據庫幫助'onCreate()'。請張貼這些。 – laalto 2014-10-26 20:20:47
向您展示數據庫處理程序方法實現...您沒有正確初始化它 – 2014-10-26 22:07:00