我想從我的應用程序中的三個EditText框中輸入文本並將它們存儲到數據庫中。我遵循教程來創建數據庫,但不知道如何查看它是否有效。我知道,當我點擊我的提交按鈕發佈到數據庫時,我的應用突然停止根據android。寫入數據庫殺死應用程序
public void insertData(int name, int location, int kill){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
db.open();
cv.put(colDeptID, 1);
cv.put(colDeptName, "Sales");
db.insert(deptTable, colDeptID, cv);
cv.put(colDeptID, 2);
cv.put(colDeptName, "IT");
db.insert(deptTable, colDeptID, cv);
db.close();
}
這是我在子程序調用我的按鈕,用指定的名稱,所在位置,並殺死我的數據庫中的新變量的子程序。這裏是我的按鈕按子程序:
public void postResults(View button){
findViewById(R.id.hunters_name);
findViewById(R.id.location);
findViewById(R.id.results);
int kill = R.id.results;
int location = R.id.location;
int name = R.id.hunters_name;
DatabaseHelper dbHelp = new DatabaseHelper(null);
dbHelp.insertData(name, location, kill);
}
我不知道爲什麼我的應用程序被殺死。
的logcat:
FATAL EXCEPTION: main
01-10 21:15:50.568: E/AndroidRuntime(973): java.lang.IllegalStateException: Could not execute method of the activity
01-10 21:15:50.568: E/AndroidRuntime(973): at android.view.View$1.onClick(View.java:3597)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.view.View.performClick(View.java:4202)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.view.View$PerformClick.run(View.java:17340)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.os.Handler.handleCallback(Handler.java:725)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.os.Looper.loop(Looper.java:137)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-10 21:15:50.568: E/AndroidRuntime(973): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 21:15:50.568: E/AndroidRuntime(973): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 21:15:50.568: E/AndroidRuntime(973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-10 21:15:50.568: E/AndroidRuntime(973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-10 21:15:50.568: E/AndroidRuntime(973): at dalvik.system.NativeStart.main(Native Method)
01-10 21:15:50.568: E/AndroidRuntime(973): Caused by: java.lang.reflect.InvocationTargetException
01-10 21:15:50.568: E/AndroidRuntime(973): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 21:15:50.568: E/AndroidRuntime(973): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.view.View$1.onClick(View.java:3592)
01-10 21:15:50.568: E/AndroidRuntime(973): ... 11 more
01-10 21:15:50.568: E/AndroidRuntime(973): Caused by: java.lang.NullPointerException
01-10 21:15:50.568: E/AndroidRuntime(973): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
01-10 21:15:50.568: E/AndroidRuntime(973): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
01-10 21:15:50.568: E/AndroidRuntime(973): at com.example.test.DatabaseHelper.insertData(DatabaseHelper.java:77)
01-10 21:15:50.568: E/AndroidRuntime(973): at com.example.test.MainActivity.postResults(MainActivity.java:77)
01-10 21:15:50.568: E/AndroidRuntime(973): ... 14 more
我看到NullPointerException異常,但我不知道如何解決它。
構造函數: public DatabaseHelper(上下文上下文)super(context,dbName,null,33); }
的onCreate: @Override 公共無效的onCreate(SQLiteDatabase DB){// TODO自動生成方法存根
db.execSQL("CREATE TABLE "+deptTable+" ("+colDeptID+ " INTEGER PRIMARY KEY , "+
colDeptName+ " TEXT)");
db.execSQL("CREATE TABLE "+employeeTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colName+" TEXT, "+colAge+" Integer, "+colDept+" INTEGER NOT NULL ,FOREIGN KEY ("+colDept+") REFERENCES"+deptTable+" ("+colDeptID+"));");
db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
" BEFORE INSERT "+
" ON "+employeeTable+
" FOR EACH ROW BEGIN"+
" SELECT CASE WHEN ((SELECT "+colDeptID+" FROM "+deptTable+"WHERE "+colDeptID+"=new."+colDept+") IS NULL)"+
" THEN RAISE (ABORT,'Foreign Key Violation') END;"+
" END;");
db.execSQL("CREATE VIEW "+viewEmps+
" AS SELECT "+employeeTable+"."+colID+" AS _id,"+
" "+employeeTable+"."+colName+","+
" "+employeeTable+"."+colAge+","+
" "+deptTable+"."+colDeptName+""+
" FROM "+employeeTable+" JOIN "+deptTable+
" ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
);
//Inserts pre-defined departments
// InsertDepts(db);
}
你需要能夠看到聲明Android告訴你有關失敗的信息。如果你知道如何查看日誌,那就做。如果你不這樣做,那麼找出如何看待日誌需要成爲你的下一步。 Android會告訴你問題是什麼(缺少數據庫?缺少表/列?等),但你需要知道如何找到它。 – prprcupofcoffee