Android的错误:不能执行此操作,因为连接池已关闭 [英] Android error: Cannot perform this operation because the connection pool has been closed

查看:196
本文介绍了Android的错误:不能执行此操作,因为连接池已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查计算器和网络为回答这个问题,但我无法找到答案。当我在姜饼运行我的应用程序,它运行良好。但是,当我在4.2.2运行它,我得到这个错误: java.lang.IllegalStateException不能执行此操作,因为连接池已关闭 我有一个SherlockFragmentActivity其中包含两个片段。

片段1:

 公开最后一类TodoFragment扩展SherlockListFragment {
NotesDbAdapter分贝;

私有静态最终诠释DELETE_ID = Menu.FIRST + 1;

公共静态TodoFragment的newInstance(String s)将{
    TodoFragment片段=新TodoFragment();

    返回片段;
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    DB =新NotesDbAdapter(getActivity());

}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
    如果(集装箱== NULL){
        返回null;
    }

    返回LL;

}
 

片段2:

 公开最后一类NotesFragment扩展SherlockListFragment {
NotesDbAdapter分贝;
私有静态最终诠释ACTIVITY_EDIT = 1;

私有静态最终诠释DELETE_ID = Menu.FIRST + 1;

公共静态NotesFragment的newInstance(字符串内容){
    NotesFragment片段=新NotesFragment();

    返回片段;
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    DB =新NotesDbAdapter(getActivity());

    db.open();
    如果(db.fetchAllNotes()。getCount将()== 0){
        doWelcomeMessage();
    }
    db.close();

}

私人无效doWelcomeMessage(){
    //显示欢迎对话框
    startActivity(新意图(getActivity(),NotesWelcome.class));

}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
    如果(集装箱== NULL){
        返回null;
    }

    DB =新NotesDbAdapter(getActivity());

    db.open();
    如果(db.fetchAllNotes()。getCount将()== 0){
        doWelcomeMessage();
    }
    db.close();


    返回LL;

}
 

SherlockFragmentActivity:

 公共类NotesFragmentActivity扩展SherlockFragmentActivity {

ViewPager mViewPager;
TabsAdapter mTab​​sAdapter;
TextView的tabCenter;
TextView的tabText;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    mViewPager =新ViewPager(本);
    mViewPager.setId(R.id.pager);

    的setContentView(mViewPager);
    动作条酒吧= getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter =新TabsAdapter(这一点,mViewPager);

    mTabsAdapter.addTab(bar.newTab()的setText(票据),NotesFragment.class,
            空值);
    mTabsAdapter.addTab(bar.newTab()的setText(待办事项列表),
            TodoFragment.class,NULL);

}

公共静态类TabsAdapter扩展FragmentPagerAdapter工具
        ActionBar.TabListener,ViewPager.OnPageChangeListener {
    私人最终语境mContext;
    私人最终动作条mActionBar;
    私人最终ViewPager mViewPager;
    私人最终的ArrayList< TabInfo> mTabs =新的ArrayList< TabInfo>();

    静态final类TabInfo {
        私人最终类别<> CLSS;
        私人最终捆绑的args;

        TabInfo(类<> _class,捆绑_args){
            CLSS = _class;
            的args = _args;
        }
    }

    公共TabsAdapter(SherlockFragmentActivity活动,ViewPager寻呼机){
        超级(activity.getSupportFragmentManager());
        mContext =活动;
        mActionBar = activity.getSupportActionBar();
        mViewPager =寻呼机;
        mViewPager.setAdapter(本);
        mViewPager.setOnPageChangeListener(本);
    }

    公共无效addTab(ActionBar.Tab选项卡,类<> CLSS,捆绑参数){
        TabInfo信息=新TabInfo(CLSS,参数);
        tab.setTag(信息);
        tab.setTabListener(本);
        mTabs.add(信息);
        mActionBar.addTab(标签);
        notifyDataSetChanged();
    }

    @覆盖
    公众诠释getCount将(){
        返回mTabs.size();
    }

    @覆盖
    公共片段的getItem(INT位置){
        TabInfo信息= mTab​​s.get(位置);
        返回Fragment.instantiate(mContext,info.clss.getName(),
                info.args);
    }

    公共无效onPageScrolled(INT位置,浮positionOffset,
            INT positionOffsetPixels){
    }

    公共无效onPageSelected(INT位置){
        mActionBar.setSelectedNavigationItem(位置);
    }

    公共无效onPageScrollStateChanged(INT状态){
    }

    公共无效onTabSelected(TAB键,FragmentTransaction英尺){
        对象标记= tab.getTag();
        的for(int i = 0; I< mTab​​s.size();我++){
            如果(mTabs.get(ⅰ)==标签){
                mViewPager.setCurrentItem(ⅰ);
            }
        }
    }

    公共无效onTabUnselected(TAB键,FragmentTransaction英尺){
    }

    公共无效onTabReselected(TAB键,FragmentTransaction英尺){
    }
}
}
 

我一直停留在这个对于小时(不笑话),并会真的AP preciate任何帮助。 再次感谢的。

解决方案

SQLiteConnectionPooling中添加了安卓4.1后的

。而在previous版本游标对象将只是试图重新打开的数据库,如果查询或其他操作提出,被关闭的连接上,使用连接池,将引发异常。该NotesDBAdapter可能是这可能与4.2.2破兼容性倒退。

I have searched StackOverflow and the web for an answer to this question, but I could not find an answer. When I run my application on Gingerbread, it runs fine. But when I run it on 4.2.2, I get this error: java.lang.IllegalStateException Cannot perform this operation because the connection pool has been closed I have a SherlockFragmentActivity which contains two fragments.

Fragment 1:

public final class TodoFragment extends SherlockListFragment {
NotesDbAdapter db;

private static final int DELETE_ID = Menu.FIRST + 1;

public static TodoFragment newInstance(String s) {
    TodoFragment fragment = new TodoFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new NotesDbAdapter(getActivity());

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    return ll;

}

Fragment 2:

public final class NotesFragment extends SherlockListFragment {
NotesDbAdapter db;
private static final int ACTIVITY_EDIT = 1;

private static final int DELETE_ID = Menu.FIRST + 1;

public static NotesFragment newInstance(String content) {
    NotesFragment fragment = new NotesFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new NotesDbAdapter(getActivity());

    db.open();
    if (db.fetchAllNotes().getCount() == 0) {
        doWelcomeMessage();
    }
    db.close();

}

private void doWelcomeMessage() {
    // Show welcome dialog
    startActivity(new Intent(getActivity(), NotesWelcome.class));

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    db = new NotesDbAdapter(getActivity());

    db.open();
    if (db.fetchAllNotes().getCount() == 0) {
        doWelcomeMessage();
    }
    db.close();


    return ll;

}

SherlockFragmentActivity:

public class NotesFragmentActivity extends SherlockFragmentActivity {

ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(bar.newTab().setText("Notes"), NotesFragment.class,
            null);
    mTabsAdapter.addTab(bar.newTab().setText("Todo List"),
            TodoFragment.class, null);

}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}
}

I've been stuck on this for HOURS (no jokes), and would really appreciate any help. Thanks again.

解决方案

SQLiteConnectionPooling was added in after Android 4.1. Whereas in previous versions a cursor object would simply try to reopen a database if a query or other operation was made on a connection that was closed, with Connection Pooling, an exception is thrown. The NotesDBAdapter is probably a throwback which may have broken compatibility with 4.2.2.

这篇关于Android的错误:不能执行此操作,因为连接池已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆