好吧,我很難過。我得到一個錯誤,eclipse/android說我的應用崩潰了,因爲表中不存在一列。問題是,我沒有使用它引用的列ID,我找不到任何理由這樣做。強制關閉光標,因爲不存在的列
在我的數據庫創建語句:
private static final String CREATE_HISTORY_TABLE=
" create table if not exists " + HISTORY_TABLE +
" (id integer primary key autoincrement," +
" DebtName text, Date date, Payment real)";
在我的onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.paymenthistory_dialog);
Bundle extras = getIntent().getExtras();
String debtname = extras.getString(DbAdapter.KEY_DEBT);
String[] from = new String[]{ DbAdapter.KEY_HISTORY_DATE, DbAdapter.KEY_HISTORY_PAYMENT};
int[] to = new int[]{R.id.PAYMENTDATE, R.id.PAYMENTDATE};
ListView paymenthistory = (ListView)findViewById(R.id.paymenthistorylist);
DatabaseHelper db = new DatabaseHelper(this);
Cursor PaymentsCursor = db.getReadableDatabase().rawQuery("SELECT * FROM tblPaymentHistory WHERE DebtName = '"+debtname+"'", null);
---> SimpleCursorAdapter HistoryAdapter = new SimpleCursorAdapter(PaymentHistory.this, R.layout.paymenthistoryrow, PaymentsCursor, from, to);
paymenthistory.setAdapter(HistoryAdapter);
}
帶箭頭的線是有錯誤的人。我已經在sqlite瀏覽器中打開了數據庫,並且它有「id」作爲列名,但是當我測試我的活動時,我總是得到以下錯誤:
11-18 00:26: 40.775:E/AndroidRuntime(18419):java.lang.IllegalArgumentException異常:致列 '_id' 不存在
其實據我所知在Android中創建數據庫時,您需要將主鍵的名稱命名爲_id,這是更好的選擇。在這種情況下,android瞭解它是一個主鍵。所以試着重新命名它並再次運行你的代碼。我認爲它應該起作用。 –