我正在尝试从我的应用程序中的三个 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();
}
这是我在子例程中为按钮调用的子例程,以及为数据库中的新变量分配名称、位置和 kill。这是我的按钮按下子程序:
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);
}
我不知道为什么我的应用程序被杀死了。
日志猫:
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(Context context) { super(context, dbName, null,33); }
onCreate: @Override public void 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);
}