无法从已填充的位于资产文件夹中的SQLite数据库获取数据。

huangapple go评论76阅读模式
英文:

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&lt;ItemRecipe&gt; itemList = new ArrayList&lt;&gt;();
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 = &quot;recipes.db&quot;;
public static final String DATABASE_location = Environment.getDataDirectory()+&quot;data/com.recipes.cook/databases/&quot;;
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 &amp;&amp; mdatabase.isOpen()){
return;
}
mdatabase=SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase() {
if (mdatabase != null) {
mdatabase.close();
}
}
public ArrayList&lt;ItemRecipe&gt; get_all_name (){
ItemRecipe itemRecipe=null;
ArrayList&lt;ItemRecipe&gt; recipslist=new ArrayList&lt;&gt;();
openDatabase();
Cursor res = mdatabase.rawQuery(&quot;select * from recipes&quot;,null);
res.moveToFirst();
while(!res.isAfterLast()){
String recipe =res.getString(res.getColumnIndex(&quot;name&quot;));
String img=res.getString(res.getColumnIndex(&quot;image&quot;));
String time = res.getString(res.getColumnIndex(&quot;time&quot;));
int rating = res.getInt(res.getColumnIndex(&quot;rating&quot;));
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&lt;ItemRecipe&gt; itemList = new ArrayList&lt;&gt;();
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());
}
}

huangapple
  • 本文由 发表于 2020年5月29日 18:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/62083421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定