我再次遇到关于由数据库填充的 ListView 的问题。我正在尝试为数据库创建一个 OnClickListener,这对我来说似乎是正确的,但是代码会产生非法状态异常。
代码:
private void getData() {
db.open();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllSubjects(),
new String[] { "subject" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id)
{
Cursor cursor = db.getSubject(id);
String subject = null;
try {
subject = cursor.getString(cursor.getColumnIndex("subject"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(Main.this, Marks.class);
intent.putExtra("selected", subject);
startActivity(intent);
db.close();
}
});
}
日志猫:
10-15 15:41:10.674: E/AndroidRuntime(23675): FATAL EXCEPTION: main
10-15 15:41:10.674: E/AndroidRuntime(23675): java.lang.IllegalStateException: database /data/data/maturaarbeit.nicola_pfister.marks/databases/database (conn# 0) already closed
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
10-15 15:41:10.674: E/AndroidRuntime(23675): at maturaarbeit.nicola_pfister.marks.database.DBAdapter.getSubject(DBAdapter.java:137)
10-15 15:41:10.674: E/AndroidRuntime(23675): at maturaarbeit.nicola_pfister.marks.Main$1.onItemClick(Main.java:59)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.widget.AbsListView$1.run(AbsListView.java:3168)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.os.Handler.handleCallback(Handler.java:605)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.os.Looper.loop(Looper.java:137)
10-15 15:41:10.674: E/AndroidRuntime(23675): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-15 15:41:10.674: E/AndroidRuntime(23675): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 15:41:10.674: E/AndroidRuntime(23675): at java.lang.reflect.Method.invoke(Method.java:511)
10-15 15:41:10.674: E/AndroidRuntime(23675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-15 15:41:10.674: E/AndroidRuntime(23675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-15 15:41:10.674: E/AndroidRuntime(23675): at dalvik.system.NativeStart.main(Native Method)
谢谢你的帮助!