我现在正在寻找解决以下问题的解决方案:
我正在使用SQLite数据库进行Android应用程序:
我的OpenHelper扩展了SQLiteOpenHelper
public OpenHelper(Context context) {
super(context, "ukra.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL("CREATE TABLE client_version (integer myID primary key)");
database.execSQL("INSERT INTO client_version VALUES ('0')");
}
openHelper由我的类DatabaseLoader使用:
private Context context;
private SQLiteDatabase db;
private OpenHelper openHelper;
public DatabaseLoader(Context context){
//Constructor
this.context = context;
this.openDB();
}
public void openDB(){
this.openHelper = new OpenHelper(this.context);
System.out.println("database = " +openHelper);
try{
db = this.openHelper.getWritableDatabase();
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("database = " +db);
}
我在LevelCoordinator中创建了DatabaseLoader:
private LevelCoordinator(Context context) {
this.context = context;
this.dbLoader = new DatabaseLoader(this.context);
this.level = new boolean[] { true, false, false };
}
public static LevelCoordinator getInstance(Context context) {
if (INSTANCE == null){
INSTANCE = new LevelCoordinator(context);
}
return INSTANCE;
}
LevelCoordinator从活动中获取上下文:
public class UkRA extends Activity {
private LevelCoordinator lc = LevelCoordinator.getInstance(this);
//...
}
问题是调用getWritableDatabase引发的NullpointerException
W/System.err( 359): java.lang.NullPointerException
W/System.err( 359): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
W/System.err( 359): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
W/System.err( 359): at de.htw.f4.ai.s4.logic.database.DatabaseLoader.openDB(DatabaseLoader.java:28)
W/System.err( 359): at de.htw.f4.ai.s4.logic.database.DatabaseLoader.<init>(DatabaseLoader.java:21)
W/System.err( 359): at de.htw.f4.ai.s4.logic.LevelCoordinator.<init>(LevelCoordinator.java:26)
W/System.err( 359): at de.htw.f4.ai.s4.logic.LevelCoordinator.getInstance(LevelCoordinator.java:35)
W/System.err( 359): at de.htw.f4.ai.s4.UkRA.<init>(UkRA.java:17)
W/System.err( 359): at java.lang.Class.newInstanceImpl(Native Method)
W/System.err( 359): at java.lang.Class.newInstance(Class.java:1429)
W/System.err( 359): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
W/System.err( 359): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
W/System.err( 359): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
W/System.err( 359): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
W/System.err( 359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
W/System.err( 359): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 359): at android.os.Looper.loop(Looper.java:123)
W/System.err( 359): at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err( 359): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 359): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err( 359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err( 359): at dalvik.system.NativeStart.main(Native Method)
有谁知道为什么有NullPointer以及我如何解决它?
答案 0 :(得分:0)
<application
android:requestLegacyExternalStorage="true" >
</application>
在 AndroidManifest文件中启用LegacyExternalStorage
,默认情况下为FALSE
。
也通过
Environment.getExternalStorageDirectory().getPath()+"/ukra.db"
代替"/ukra.db"
它可能会对您有所帮助。