ListView由数据库填充;项目选择会导致运行时异常

时间:2012-10-15 15:50:03

标签: android database listview illegalstateexception

我再次遇到有关由数据库填充的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();
        }
    });
}

logcat的:

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)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

那是因为您在onItemClick中关闭了数据库。因此,当您再次单击onItemClick时再次调用但数据库已被关闭时。