诚信求一个答案,能解决问题必定采纳!
我定义了数据库DBHelper继承SQLiteOpenHelper,MainActivity里放置四个滑动的fragment,里面fragment要打开数据库要怎么写?context一直填不对。
fragment有空的构造方法CommunityFragment()、自带的onCreateView(),还有一个自己写的onActivityCreated()方法,通过getActivity().findViewById()获取控件的代码都写在onActivityCreated()方法里。
我想知道DBHelper dbHelper = new DBHelper(context);这句应该写在哪里?是最外层还是哪个方法内部?
上面的context应该填什么?
我写在最外层,然后填getActivity().getApplicationContext()、getContent()没有成功
写在onActivityCreated()里,再填getActivity().getApplicationContext()、getContent()还是没有成功。
DBHelper代码:
public class DBHelper extends SQLiteOpenHelper {
//声明数据库名,版本号,表名
private static final String DB_NAME = "DB_ChangShou.db";
private static final int DB_VERSION = 1;
public static final String table_Accounts = "Accounts";
public DBHelper(Context context) {
super(context,DB_NAME,null,DB_VERSION);//上下文,数据库名,空,版本号
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建用户信息数据表Accounts:_id,账号,昵称,密码
String sql = "CREATE TABLE IF NOT EXISTS "+DBHelper.table_Accounts+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,account VARCHAR(20),name VARCHAR(20),password VARCHAR(10))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity代码:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener {
ViewPager viewPager;
BottomNavigationView bottomNavigationView;
HomeFragment homeFragment = new HomeFragment();
CommunityFragment communityFragment = new CommunityFragment();
MallFragment mallFragment = new MallFragment();
MineFragment mineFragment = new MineFragment();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();//初始化
}
private void init() {
//获取页面标签对象
viewPager = findViewById(R.id.viewPager);
viewPager.setOnPageChangeListener(this);//添加页面切换监视器
bottomNavigationView = findViewById(R.id.bottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(this);//添加导航栏选择监视器
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {//给页面添加适配器
@NonNull
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:return homeFragment;
case 1:return communityFragment;
case 2:return mallFragment;
case 3:return mineFragment;
}
return null;
}
@Override
public int getCount() {//界面个数
return 4;
}
});
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
@Override
public void onPageSelected(int position) {
bottomNavigationView.getMenu().getItem(position).setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) { }
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
viewPager.setCurrentItem(item.getOrder());
return true;
}
}
fragment代码:
public class MineFragment extends Fragment {
public MineFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mine, container, false);
// Inflate the layout for this fragment
return view;
}
//前面代码是创建fragment时自动生成的,删除了多余的,上面两个方法没有动,自己写的代码在onActivityCreated()里
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// 这里数据库没有打开,显示空指针错误,可能是context出了问题
DBHelper dbHelper = new DBHelper(getContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();
// 点击“修改身材”按钮
//控件都是通过加上getActivity()获取的
Button btnModifyFigure = getActivity().findViewById(R.id.mine_button_modifyFigure);
btnModifyFigure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), ModifyFigureActivity.class);
startActivity(intent);
}
});
}
}