Android:java.lang.IllegalStateException求助大神~万分感谢

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: select Fee,Budget,Time,Type from finance

 public class BillActivity extends Activity {
    private ListView billListView;
    private MySQLiteOpenHelper sqLiteOpenHelper;
    private SQLiteDatabase mDataBase;

    // 存储数据的数组列表
    ArrayList<HashMap<String, Object>> listData;
    // 适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bill);

        sqLiteOpenHelper = new MySQLiteOpenHelper(BillActivity.this, "finance.db", null, 1);
        mDataBase = sqLiteOpenHelper.getWritableDatabase();
        Cursor c = mDataBase.rawQuery("select * from finance", null);
        Log.i("database", "data" + mDataBase.isOpen());
        int columnsSize = c.getColumnCount();
        listData = new ArrayList<HashMap<String, Object>>();
        // 获取表的内容

            while (c.moveToNext()) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                for (int i = 0; i < columnsSize; i++) {
                    map.put("Type", c.getString(c.getColumnIndex("Type")));  //Type time fee budget
                    map.put("Time", c.getString(c.getColumnIndex("Time")));
                    String budget = c.getString(c.getColumnIndex("Budget"));
                    if (budget.equals("收入")) {
                        map.put("Fee", "+" + c.getString(c.getColumnIndex("Fee")));
                    } else {
                        map.put("Fee", "-" + c.getString(c.getColumnIndex("Fee")));
                    }
                    map.put("Budget", c.getString(c.getColumnIndex("Budget")));


                    Log.i("data", "data= " + map.get("Time"));
                    Log.i("data", "data= " + map.get("Type"));
                    Log.i("data", "data= " + map.get("Budget"));
                    Log.i("data", "data= " + map.get("Fee"));
                }
                listData.add(map);
                c.close();
//            mDataBase.close();
//            sqLiteOpenHelper.close();
            }


        billListView = (ListView) findViewById(R.id.bill_listView);
        SimpleAdapter billAdapter = new SimpleAdapter(BillActivity.this,
                listData,// 数据源
                R.layout.bill_item,// ListItem的XML实现
                // 动态数组与Item对应的子项
                new String[]{"Time", "Fee", "Budget", "Type"},
                // ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[]{R.id.billItem_time, R.id.billItem_money, R.id.billItem_budget, R.id.billItem_type});
        billListView.setAdapter(billAdapter);
    }

读取数据库的时候抛出异常

 FATAL EXCEPTION: main
                                                                    Process: com.chase.cn.demon, PID: 25579
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chase.cn.demon/com.chase.cn.demon.BillActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: select Fee,Budget,Time,Type from finance
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: select Fee,Budget,Time,Type from finance
                                                                        at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                                                                        at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
                                                                        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:152)
                                                                        at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:124)
                                                                        at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:214)
                                                                        at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
                                                                        at com.chase.cn.demon.BillActivity.onCreate(BillActivity.java:41)
                                                                        at android.app.Activity.performCreate(Activity.java:5231)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 

4个回答

c.close 放在while外面

c.close(); 在while (c.moveToNext())循环里了,关闭数据库应该在循环结束,你就读了一次就关闭了;

图片说明

谢谢大神们 我自己粗心了 万分感谢~

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问