程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> adapter-Android:java.lang.IllegalStateException求助大神~萬分感謝

adapter-Android:java.lang.IllegalStateException求助大神~萬分感謝

編輯:編程解疑
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) 

最佳回答:


c.close 放在while外面

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved