在使用来自 SQLite 数据库的数据的片段状态寻呼机适配器实现滑动界面期间崩溃

Crash during implementation of swipe interface using fragment state pager adapter with data from SQLite database

我正在尝试实现一个滑动界面来滑动浏览包含图书列表的数据库。由于它来自数据库,因此我使用了 FragmentStatePagerAdapter。代码显示没有编译错误,但是一旦我多次滑动,应用程序就会崩溃 代码:

public class swipeActivity extends AppCompatActivity {
//static int num_items;

bookAdapter mAdapter;
ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipe);

    mAdapter = new bookAdapter(getSupportFragmentManager(), this);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);


}

public static class bookAdapter extends FragmentStatePagerAdapter {
    private Context context;

    public bookAdapter(FragmentManager fm, Context c) {
        super(fm);
        context = c;
    }

    @Override
    public int getCount() {
        DBHandler db = new DBHandler(context);
        return db.getBookCount();
    }

    @Override
    public Fragment getItem(int position) {

        return ArrayListFragment.newInstance(position);
    }

}

public static class ArrayListFragment extends ListFragment {
    int mNum;
    private Cursor cursor;

    //ListView bookList;
    static ArrayListFragment newInstance(int num) {
        ArrayListFragment f = new ArrayListFragment();
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        DBHandler db = new DBHandler(getContext());
        cursor = db.getCursor();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        cursor.moveToPosition(mNum);
        View v = inflater.inflate(R.layout.fragment_swipe, container, false);
        View tv = v.findViewById(R.id.textTitle);
        ((TextView) tv).setText(cursor.getString(1));
        tv = v.findViewById(R.id.textAuth);
        ((TextView) tv).setText(cursor.getString(2));
        tv = v.findViewById(R.id.textNos);
        ((TextView) tv).setText("Copies : " + cursor.getString(4));
        cursor.close();

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /* stuff*/

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("FragmentList", "Item clicked: " + id);
    }
}
}

错误日志:

07-10 11:39:09.758 4081-4081/com.example.ajay_5674.library1 E/SQLiteLog: (14) cannot open file at line 32456 of [bda77dda96]

07-10 11:39:09.758 4081-4081/com.example.ajay_5674.library1 E/SQLiteLog: (14) os_unix.c:32456: (24) open(/data/user/0/com.example.ajay_5674.library1/databases/libraryInfo1-journal) - 07-10 11:39:09.758 4081-4081/com.example.ajay_5674.library1 E/SQLiteLog: (14) cannot open file at line 32456 of [bda77dda96]

07-10 11:39:09.758 4081-4081/com.example.ajay_5674.library1 E/SQLiteLog: (14) os_unix.c:32456: (24) open(/data/user/0/com.example.ajay_5674.library1/databases/libraryInfo1-journal) -

07-10 11:39:09.758 4081-4081/com.example.ajay_5674.library1 E/SQLiteLog: (14) statement aborts at 12: [SELECT * FROM library] unable to open database file 07-10 11:39:09.759 4081-4081/com.example.ajay_5674.library1 E/SQLiteQuery: exception: unable to open database file (code 14); query: SELECT * FROM library

07-10 11:39:09.759 4081-4081/com.example.ajay_5674.library1 D/AndroidRuntime: Shutting down VM

07-10 11:39:09.759 4081-4081/com.example.ajay_5674.library1 E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.ajay_5674.library1, PID: 4081 android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)

编辑:发现问题。我专注于片段创建期间的查询,而在项目计数期间我没有注意到查询!固定为:

public static class bookAdapter extends FragmentStatePagerAdapter {
    private Context context;
    int num_items;
    public bookAdapter(FragmentManager fm, Context c) {
        super(fm);
        context = c;
        DBHandler db = new DBHandler(context);
        num_items = db.getBookCount();
        db.close();
    }

    @Override
    public int getCount() {

        //Log.d("Book Count :",Integer.toString(db.getBookCount()));
        return num_items;
    }

    @Override
    public Fragment getItem(int position) {

        Log.d("Book Position :",Integer.toString(position));
        return ArrayListFragment.newInstance(position);

    }

}

实际问题是 android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14),这是因为您正在创建 SQLite 数据库的多个实例。

您每次都在需要时创建一个 DBHandler。尝试减少 DBHandler 的实例。

并且您还在 onCreateView 中关闭光标并在 onCreate() 中创建它。这不好 practice.If 你在 onCreate 中打开游标,你应该在 onDestory() 中关闭它。