我通过 viewpager 有一个带有两个不同视图(片段)的 swipeview。我现在希望这两个页面显示来自 sqllite 数据库的不同数据。问题是我无法弄清楚如何做到这一点,从主要活动我无法访问我的片段上的视图(TableLayout)以及从片段源代码我无法访问数据库,因为适配器不会打开(适配器的上下文是超级.getActivity())。有没有办法做到这一点?
片段代码:
DBAdapter adapter = new DBAdapter(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(container==null){
return null;
}
layout=(RelativeLayout)inflater.inflate(R.layout.layout_morning, container, false);
adapter.open();
adapter.close();
DBAdapter 的代码:
private static String DB_NAME = "database.dat";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
if (db!=null){
db.close();
}
DBHelper.close();
}
错误是 NullPointerException