当我调用方法getWritableDatabase
时,我抛出异常。阅读一些类似的问题,我认为是关于上下文的问题,但我不知道为什么。
这是LOGCAT:
07-22 09:33:57.534: E/AndroidRuntime(406): FATAL EXCEPTION: main
07-22 09:33:57.534: E/AndroidRuntime(406): java.lang.NullPointerException
07-22 09:33:57.534: E/AndroidRuntime(406): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
07-22 09:33:57.534: E/AndroidRuntime(406): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.example.login.DBEntrenamiento.addEntrenamiento(DBEntrenamiento.java:26)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.example.login.Entrenamiento.saveEntrenamiento(Entrenamiento.java:136)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.example.login.Entrenamiento.access$2(Entrenamiento.java:127)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.example.login.Entrenamiento$2.onExecuteHttpPostAsyncListener(Entrenamiento.java:106)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.example.login.HttpClientManager$1.run(HttpClientManager.java:129)
07-22 09:33:57.534: E/AndroidRuntime(406): at android.os.Handler.handleCallback(Handler.java:587)
07-22 09:33:57.534: E/AndroidRuntime(406): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 09:33:57.534: E/AndroidRuntime(406): at android.os.Looper.loop(Looper.java:123)
07-22 09:33:57.534: E/AndroidRuntime(406): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-22 09:33:57.534: E/AndroidRuntime(406): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 09:33:57.534: E/AndroidRuntime(406): at java.lang.reflect.Method.invoke(Method.java:521)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-22 09:33:57.534: E/AndroidRuntime(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-22 09:33:57.534: E/AndroidRuntime(406): at dalvik.system.NativeStart.main(Native Method)
以下是抛出异常的代码:
public DBEntrenamiento(Context context) {
super(context);
}
public void addEntrenamiento(String nombre, String descripcion, String tipo) {
SQLiteDatabase db = this.getWritableDatabase();
if(db != null ){
ContentValues values = new ContentValues();
values.put(KEY_NOMBRE, nombre);
values.put(KEY_DESCRIPCION, descripcion);
values.put(KEY_TIPO, tipo);
db.insert(Table, null, values);
db.close();
}
else{
String error = "error de apertura db";
}
}
这是帮手
public class bbdd extends SQLiteOpenHelper{
private static String name = "android";
private static int version = 1;
private static CursorFactory cursorFactory = null;
public bbdd(Context context){
super(context,name,cursorFactory, version);
}
private String SQLCreateLogin = "CREATE TABLE login("
+ " id INTEGER PRIMARY KEY,"
+ " name TEXT,"
+ " email TEXT UNIQUE,"
+ " uid TEXT,"
+ " created_at TEXT" + ")";
private String SQLCreateEntrenamiento = "CREATE TABLE entrenamiento("
+ " id INTEGER PRIMARY KEY,"
+ " nombre TEXT,"
+ " descripcion TEXT,"
+ " tipo TEXT,"
+ " created_at TEXT" + ")";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SQLCreateLogin);
db.execSQL(SQLCreateEntrenamiento);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS entrenamiento");
//Se crea la nueva versión de la tabla
db.execSQL(SQLCreateEntrenamiento);
}
}
答案 0 :(得分:0)
试试这个:
SQLiteDatabase db;
将db变量作为类(全局)变量并在
中初始化它public DBEntrenamiento(Context context) {
super(context);
db = this.getWritableDatabase();
}
并从方法
中删除db初始化public void addEntrenamiento(String nombre, String descripcion, String tipo) {
}