0

我正在尝试从我的应用程序中的三个 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);  
 }
4

3 回答 3

0

检查您的 Logcat 输出。您可能会在那里看到一个 NULLPointer 异常。

$>adb logcat(是您查看日志的方式)。

于 2013-01-10T21:38:34.890 回答
0

你的方法 postResult 应该做什么?因为

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;

给你一些类似指向这个视图的指针。

如果在您的 xml 中,您有:

 <EditText android:id="@+id/foobar"

然后会自动在 R.id 中创建指向此 EditText 的指针。

R.id.foobar  

只给你数值(识别正确的资源 - 你的editText)

要获得此资源,您应该使用:

View v = findViewById(R.id.foobar);

如果是 EditText,您可以使用演员表:

EditText et = (EditText) findViewById(R.id.foobar) 

如果在此 editText 中您有价值并想要读取此值:

int kill = et.getText();
于 2013-01-10T21:38:44.477 回答
0

可能您需要将上下文作为 DatabaseHelper 的参数。改变:

DatabaseHelper dbHelp = new DatabaseHelper(null);

为了:

DatabaseHelper dbHelp = new DatabaseHelper(getApplicationContext());

如果没有帮助,请显示您从 DatabaseHelper 类中使用的构造函数、onCreate 方法和创建语句

于 2013-01-10T22:23:43.793 回答