Chase_Java 2016-11-10 10:16 采纳率: 30%
浏览 2895
已采纳

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条回答 默认 最新

  • ImportSUC 2016-11-10 10:34
    关注

    c.close 放在while外面

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?