视图寻呼机中的DatabaseObjectNotClosedException

时间:2012-07-18 15:19:02

标签: android database cursor android-viewpager

我有一个自定义水平视图分页器,用于翻阅列表视图,每个视图都是从数据库表中的条目填充的。列表视图与数据库中的表一样多。现在的问题是我得到了DatabaseObjectNotClosedException。我在DbHelper中有三个相关的方法。这是我的DbHelper中的代码。

public int countTables() {
    int count = 0;
    String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
    Cursor cursor =DB.rawQuery(SQL_GET_ALL_TABLES, null);
    cursor.moveToFirst();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        count = cursor.getCount();
    }
    cursor.close();
    count = count - 2;
    return count;
}

public String getTableName(int count) {
    String tabname = "";
    String SQL_GET_ALL_TABLES = "SELECT * FROM SQLITE_SEQUENCE";
    Cursor cursor = DB.rawQuery(SQL_GET_ALL_TABLES, null);
    cursor.moveToPosition(count);
    tabname = cursor.getString(cursor.getColumnIndex("name"));
    cursor.close();
    DB.close();
    return tabname;
}
public Cursor getAll(String tableName) {
    return (getReadableDatabase().rawQuery("SELECT _id, note type FROM "
            + tableName, null));

}

这是我的主要活动,包含视图寻呼机类。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewp);
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);
    myPager.setAdapter(adapter);
    DbHelper helper = new DbHelper(this);
    count = helper.countTables();
    myPager.setCurrentItem(count - 1);
    temp1 = temp = helper.countTables() - 1;
private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return count;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        helper = new DbHelper(Listing.this);
        View view = null;

        if (position == count) {
            view = inflater.inflate(R.layout.middle, null);
            ListView list = (ListView) view.findViewById(R.id.listView1);
            // helper = new DbHelper(Listing.this);
            String tabName = helper.getTableName(position);
            dataset_cursor = helper.getAll(tabName);
            startManagingCursor(dataset_cursor);
            adapter5 = new NoteAdapter(dataset_cursor);
            list.setAdapter(adapter5);
            // adapter.notifyDataSetChanged();
            temp--;
            temp1--;
        }

        if (position < temp) {
            temp--;
            temp1--;
            view = inflater.inflate(R.layout.left, null);
            ListView list1 = (ListView) view.findViewById(R.id.listView1);
            // helper = new DbHelper(Listing.this);
            String tabName = helper.getTableName(position);
            dataset_cursor1 = helper.getAll(tabName);
            startManagingCursor(dataset_cursor1);
            adapter1 = new NoteAdapter(dataset_cursor1);
            list1.setAdapter(adapter1);
            // adapter1.notifyDataSetChanged();
            // Toast.makeText(getBaseContext(), tabName, Toast.LENGTH_LONG);
            temp--;
            temp1--;
        }
        if (position > temp) {
            temp++;
            temp1++;
            view = inflater.inflate(R.layout.right, null);
            ListView list2 = (ListView) view.findViewById(R.id.listView1);
            // helper = new DbHelper(Listing.this);
            String tabName = helper.getTableName(position);
            dataset_cursor2 = helper.getAll(tabName);
            startManagingCursor(dataset_cursor2);
            adapter2 = new NoteAdapter(dataset_cursor2);
            list2.setAdapter(adapter2);
            // adapter2.notifyDataSetChanged();
        }

        ((ViewPager) collection).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {

    }

}

首先执行countTable(),getTableName()是第二个。 我不能在方法countTables中使用DB.close(),因为当我使用getTableName获取表名时,活动强制关闭说数据库已经关闭。

我尝试覆盖onDestroy,onStop等。我认为它没用,因为当我通过listViews进行spaging时,Execption出现了。

如何摆脱此DatabaseObjectNotClosedException?

1 个答案:

答案 0 :(得分:0)

你可以调用你的onCreate()方法

尝试

cursor.close() <--
helper.close();