2013-04-17 34 views
9

Tôi đã tìm kiếm StackOverflow và web để có câu trả lời cho câu hỏi này, nhưng tôi không thể tìm thấy câu trả lời. Khi tôi chạy ứng dụng của tôi trên Gingerbread, nó chạy tốt. Nhưng khi tôi chạy nó trên 4.2.2, tôi nhận được lỗi này: java.lang.IllegalStateException Cannot perform this operation because the connection pool has been closed Tôi có một SherlockFragmentActivity chứa hai đoạn.Lỗi Android: Không thể thực hiện thao tác này vì nhóm kết nối đã bị đóng

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) { 
    } 
} 
} 

Tôi đã bị mắc kẹt trên này cho GIỜ (không đùa), và sẽ thực sự đánh giá cao sự giúp đỡ nào. Cảm ơn bạn lần nữa.

+0

Đăng toàn bộ stacktrace. Lỗi này có nghĩa là ứng dụng đóng db ở đâu đó và kết nối db khác xảy ra sau đó nhưng db bị đóng – kinghomer

+0

bạn phải loại bỏ hoạt động gần. thử bằng cách loại bỏ close(). lỗi này là do đóng db. – Nepster

Trả lời

8

SQLiteConnectionHãy thêm vào sau khi Android 4.1. Trong khi các phiên bản trước một đối tượng con trỏ chỉ đơn giản là cố gắng mở lại cơ sở dữ liệu nếu một truy vấn hoặc hoạt động khác được thực hiện trên một kết nối đã bị đóng, với kết nối tổng hợp, một ngoại lệ được ném. NotesDBAdapter có lẽ là một sự hủy bỏ có thể đã bị hỏng khả năng tương thích với 4.2.2.

+1

Vì vậy, làm thế nào để chúng ta giải quyết điều đó? Đôi khi tôi gặp lỗi này trong ứng dụng của mình. Khi ở một số phần của mã tôi gọi cursor.moveToFirst(), sau đó tôi nhận được lỗi: Không thể thực hiện thao tác này vì nhóm kết nối đã bị đóng. –

+0

cách bạn giải quyết nó @FerranNegre –

+0

Tôi đã viết chủ đề này http://stackoverflow.com/questions/23293572/android-cannot-perform-this-operation-because-the-connection-pool-has-been-clos/23293930 ? noredirect = 1 # 23293930. Đọc câu trả lời và nhận xét của tôi về câu trả lời đó :) –

2

Tôi gặp vấn đề tương tự, vì nhiều chủ đề truy cập cùng một SQLiteOpenHelper và do đó đã mở và đóng kết nối đồng thời.

Tôi đã giải quyết nó bằng cách thêm thuộc tính userCount vào trình trợ giúp. Bây giờ người trợ giúp chỉ đóng kết nối, nếu userCount bằng không, nghĩa là không có chủ đề nào khác hiện đang sử dụng cơ sở dữ liệu.

+0

vui lòng gửi mã !!!! –

Các vấn đề liên quan