我使用Android SQLiteAssetHelper将填充的.db文件加载到我的Android应用程序中。但是,使用以下代码,它实际上并不构成数据库。
public class DataBaseHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "quiz.db";
private static final int DATABASE_VERSION = 1;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Question getRandQuestion(String category){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String sqlTables = category;
qb.setTables(sqlTables);
Cursor c=db.rawQuery("SELECT * FROM " + category + " ORDER BY RANDOM() LIMIT 1", null);
Question question = new Question(Integer.parseInt(c.getString(0)), c.getString(1),
c.getString(2), Uri.parse(c.getString(3)));
return question;
}
}
我一直收到以下错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
我在另一个类中调用DataBaseHelper构造函数,如下所示:
Play Class:
private DataBaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
db=new DataBaseHelper(this);
然后我访问DataBaseHelper中的getRandQuestion方法,但在尝试打开可读数据库时,我得到上述错误。
关于出了什么问题的任何想法?
提前致谢
编辑:
Enitre堆栈跟踪:
01-29 18:27:02.228 3867-3867/com.example.anthony.triviaapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.anthony.triviaapp, PID: 3867
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anthony.triviaapp/com.example.anthony.TriviaApplication.Play}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
at com.example.anthony.TriviaApplication.DataBaseHelper.getRandQuestion(DataBaseHelper.java:44)
at com.example.anthony.TriviaApplication.Play.CreateQuiz(Play.java:83)
at com.example.anthony.TriviaApplication.Play.onCreate(Play.java:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)