英文:
Unable to get data from populated sqlite database in asset folder
问题
代码部分不要翻译,只返回翻译好的内容:
错误是,当我在模拟器上运行我的代码时,应用程序会因错误而停止(java.lang.RuntimeException:无法启动活动:
java.lang.NullPointerException:在代码String dbpath =mcontext.getDatabasePath(DATABASE_NAME).getPath();
中对空对象引用进行虚拟方法调用'java.io.File android.content.Context.getDatabasePath(java.lang.String)'的尝试。
myfragment:
public class FragmentHome extends Fragment {
private List<ItemRecipe> itemList = new ArrayList<>();
private RecyclerView recyclerView;
private RecipeAdapter mAdapter;
private AppCompatActivity appCompatActivity;
databaseHelper db = new databaseHelper(getActivity());
public FragmentHome() {
setHasOptionsMenu(true);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
}
sqldatabasehelper:
public class databaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "recipes.db";
public static final String DATABASE_location = Environment.getDataDirectory() + "data/com.recipes.cook/databases/";
private Context mcontext;
private SQLiteDatabase mdatabase;
public databaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void openDatabase() {
String dbpath = mcontext.getDatabasePath(DATABASE_NAME).getPath();
if (mdatabase != null && mdatabase.isOpen()) {
return;
}
mdatabase = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase() {
if (mdatabase != null) {
mdatabase.close();
}
}
public ArrayList<ItemRecipe> get_all_name() {
ItemRecipe itemRecipe = null;
ArrayList<ItemRecipe> recipslist = new ArrayList<>();
openDatabase();
Cursor res = mdatabase.rawQuery("select * from recipes", null);
res.moveToFirst();
while (!res.isAfterLast()) {
String recipe = res.getString(res.getColumnIndex("name"));
String img = res.getString(res.getColumnIndex("image"));
String time = res.getString(res.getColumnIndex("time"));
int rating = res.getInt(res.getColumnIndex("rating"));
res.moveToNext();
recipslist.add(new ItemRecipe(recipe, img, time, rating));
}
res.close();
closeDatabase();
return recipslist;
}
}
英文:
The bug is that when I run my code on emulator the app is stopping with an error (java.lang.RuntimeException: Unable to start activity :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference) in the code String dbpath =mcontext.getDatabasePath(DATABASE_NAME).getPath();
myfragment :
public class FragmentHome extends Fragment {
private List<ItemRecipe> itemList = new ArrayList<>();
private RecyclerView recyclerView;
private RecipeAdapter mAdapter;
private AppCompatActivity appCompatActivity;
databaseHelper db = new databaseHelper(getActivity());
public FragmentHome() {
setHasOptionsMenu(true);
}
public void onCreate(Bundle a) {
super.onCreate(a);
setHasOptionsMenu(true);
}
sqldatabasehelper :
public class databaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "recipes.db";
public static final String DATABASE_location = Environment.getDataDirectory()+"data/com.recipes.cook/databases/";
private Context mcontext ;
private SQLiteDatabase mdatabase;
public databaseHelper( Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void openDatabase(){
String dbpath =mcontext.getDatabasePath(DATABASE_NAME).getPath();
if (mdatabase != null && mdatabase.isOpen()){
return;
}
mdatabase=SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase() {
if (mdatabase != null) {
mdatabase.close();
}
}
public ArrayList<ItemRecipe> get_all_name (){
ItemRecipe itemRecipe=null;
ArrayList<ItemRecipe> recipslist=new ArrayList<>();
openDatabase();
Cursor res = mdatabase.rawQuery("select * from recipes",null);
res.moveToFirst();
while(!res.isAfterLast()){
String recipe =res.getString(res.getColumnIndex("name"));
String img=res.getString(res.getColumnIndex("image"));
String time = res.getString(res.getColumnIndex("time"));
int rating = res.getInt(res.getColumnIndex("rating"));
res.moveToNext();
recipslist.add(
new ItemRecipe(
recipe,img,time,rating
)
);
}
res.close();
closeDatabase();
return recipslist;
}
答案1
得分: 0
当您调用getActivity()
方法时,它会返回null,因为片段尚未附加。请在onCreate
中打开数据库。
public class FragmentHome extends Fragment {
private List<ItemRecipe> itemList = new ArrayList<>();
private RecyclerView recyclerView;
private RecipeAdapter mAdapter;
private AppCompatActivity appCompatActivity;
public FragmentHome() {
setHasOptionsMenu(true);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
databaseHelper db = new databaseHelper(getActivity());
}
}
英文:
When you call getActivity()
method it return null because the fragment is not attached yet. Open the database in onCreate
.
public class FragmentHome extends Fragment {
private List<ItemRecipe> itemList = new ArrayList<>();
private RecyclerView recyclerView;
private RecipeAdapter mAdapter;
private AppCompatActivity appCompatActivity;
public FragmentHome() {
setHasOptionsMenu(true);
}
public void onCreate(Bundle a) {
super.onCreate(a);
setHasOptionsMenu(true);
databaseHelper db = new databaseHelper(getActivity());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论