使用片段状态寻呼机适配器和来自SQLite数据库

时间:2017-07-10 06:29:46

标签: android sqlite android-fragments swipe

我正在尝试使用滑动界面来浏览包含书籍列表的数据库。由于它来自数据库,我使用了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)无法在[bda77dda96]的第32456行打开文件

     

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)无法在[bda77dda96]的第32456行打开文件

     

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)语句在12处中止:[SELECT * FROM library]无法打开数据库文件   07-10 11:39:09.759 4081-4081 / com.example.ajay_5674.library1 E / SQLiteQuery:exception:无法打开数据库文件(代码14);查询:SELECT * FROM library

     

07-10 11:39:09.759 4081-4081 / com.example.ajay_5674.library1 D / AndroidRuntime:关闭虚拟机

     

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

     
    

处理:com.example.ajay_5674.library1,PID:4081     android.database.sqlite.SQLiteCantOpenDatabaseException:无法打开数据库文件(代码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);

    }

}

1 个答案:

答案 0 :(得分:0)

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

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

您还要关闭onCreateView中的光标并在onCreate()中创建它。这不是一个好习惯。如果你在onCreate中打开游标,你应该在onDestory()中关闭它。