[英]NullPointerException in ContextWrapper.openOrCreateDatabase when using SQLiteOpenHelper
I'm making a new android application that uses databases. 我正在制作一个使用数据库的新android应用程序。 I used similar code to create and copy database, but it's not working for some reason.
我使用了类似的代码来创建和复制数据库,但是由于某种原因,它无法正常工作。 I can't figure it out, help.
我不知道,帮助。 LogCat:
logcat的:
07-03 21:38:54.260: E/AndroidRuntime(12172): FATAL EXCEPTION: main
07-03 21:38:54.260: E/AndroidRuntime(12172): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{lv.revopeng.guessthelocation/lv.revopeng.guessthelocation.LevelActivity}: java.lang.NullPointerException
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.os.Looper.loop(Looper.java:123)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-03 21:38:54.260: E/AndroidRuntime(12172): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 21:38:54.260: E/AndroidRuntime(12172): at java.lang.reflect.Method.invoke(Method.java:521)
07-03 21:38:54.260: E/AndroidRuntime(12172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-03 21:38:54.260: E/AndroidRuntime(12172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-03 21:38:54.260: E/AndroidRuntime(12172): at dalvik.system.NativeStart.main(Native Method)
07-03 21:38:54.260: E/AndroidRuntime(12172): Caused by: java.lang.NullPointerException
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:112)
07-03 21:38:54.260: E/AndroidRuntime(12172): at lv.revopeng.guessthelocation.DataBaseHelper.open(DataBaseHelper.java:114)
07-03 21:38:54.260: E/AndroidRuntime(12172): at lv.revopeng.guessthelocation.DataBaseHelper.getLastId(DataBaseHelper.java:128)
07-03 21:38:54.260: E/AndroidRuntime(12172): at lv.revopeng.guessthelocation.LevelActivity.<init>(LevelActivity.java:31)
07-03 21:38:54.260: E/AndroidRuntime(12172): at java.lang.Class.newInstanceImpl(Native Method)
07-03 21:38:54.260: E/AndroidRuntime(12172): at java.lang.Class.newInstance(Class.java:1429)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-03 21:38:54.260: E/AndroidRuntime(12172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-03 21:38:54.260: E/AndroidRuntime(12172): ... 11 more
DataBaseHelper.java: DataBaseHelper.java:
public class DataBaseHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/lv.revopeng.guessthelocation/databases/";
private static String DB_NAME = "GuessTheLocationTest.db";
public static final String TABLE_NAME1 = "gtl_levels";
public static final String WHATTODONOW_COLUMN_ID = "_id";
public static final String WHATTODONOW_COLUMN_INFO = "levelInfo";
public static final String WHATTODONOW_COLUMN_ANSWER = "levelAnswer";
public static final String WHATTODONOW_COLUMN_IMAGE = "levelImage";
public static final String WHATTODONOW_COLUMN_CLEARED = "levelCleared";
private DataBaseHelper openHelper;
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase myDataBase, int oldVersion,
int newVersion) {
}
public DataBaseHelper open() throws SQLException {
openHelper = new DataBaseHelper(myContext);
myDataBase = openHelper.getWritableDatabase();
return this;
}
public void closee() {
openHelper.close();
}
String[] columns = new String[] { WHATTODONOW_COLUMN_ID,
WHATTODONOW_COLUMN_INFO, WHATTODONOW_COLUMN_ANSWER,
WHATTODONOW_COLUMN_IMAGE, WHATTODONOW_COLUMN_CLEARED };
public int getLastId() {
int id = 0;
open();
final String MY_QUERY = "SELECT MAX(_id) AS _id FROM " + TABLE_NAME1;
Cursor mCursor = myDataBase.rawQuery(MY_QUERY, null);
try {
if (mCursor.getCount() > 0) {
mCursor.moveToFirst();
id = mCursor.getInt(0);
mCursor.close();
close();
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
}
return id;
}
On LevelActivity.java the createDataBase() method is called and on first DataBaseHelper use it gets the error. 在LevelActivity.java上,将调用createDataBase()方法,并且在首次使用DataBaseHelper时,它将获得错误。
EDIT: For me, it looks like getLastId is null, but it shouldn't be, so there are problem accessing the database. 编辑:对我来说,看起来getLastId为null,但不应这样,因此访问数据库时出现问题。 I looked at File Explorer, the database is there and info in it is correct.
我查看了文件资源管理器,数据库在那里,并且其中的信息是正确的。
Found my mistake. 发现我的错误。 I called an function that gets data from database before creating the database..
我调用了一个函数,该函数在创建数据库之前从数据库获取数据。
Remove open()
method. 删除
open()
方法。
Do not call getReadableDatabase();
不要调用
getReadableDatabase();
and/or getWritableDatabase();
和/或
getWritableDatabase();
within DataBaseHelper.java, instead work directly with the database (ie, use execSQL()
, query()
, insert()
, etc). 而是直接在DataBaseHelper.java中使用数据库(即,使用
execSQL()
, query()
, insert()
等)。 Use those methods elsewhere as dataBaseHelper.getReadableDatabase();
在其他地方使用这些方法作为
dataBaseHelper.getReadableDatabase();
, dataBaseHelper.getWritableDatabase();
,
dataBaseHelper.getWritableDatabase();
. 。
Remove DB_PATH
and do not use one. 删除
DB_PATH
,不要使用一个。 You do not need to provide the path, the system will create and read the database from your packages databases folder. 您不需要提供路径,系统将从您的packages数据库文件夹中创建并读取数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.