1

调用方法时抛出异常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);

    }

}
4

1 回答 1

0

尝试这个:

SQLiteDatabase db;

将 db 变量作为类(全局)变量并将其初始化为

public DBEntrenamiento(Context context) {
    super(context);
   db = this.getWritableDatabase();
}

并从方法中删除数据库初始化

public void addEntrenamiento(String nombre, String descripcion, String tipo) {
}
于 2013-07-27T10:54:03.957 回答