SQLiteAssetHelper扩展返回null

时间:2014-07-25 08:38:43

标签: java android

我从DatabaseHandler获取NullPointerException。

这是我的DatabaseHandler:

public class DatabaseHandler extends SQLiteAssetHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "carsGrid",
            TABLE_CARS = "cars ",
            KEY_ID = "id",
            KEY_POSTCODE = "postcode",
            KEY_ADDRESS = "address",
            KEY_IMAGE = "image";


        public DatabaseHandler(Context context) {
            //super(context, DATABASE_NAME, null, DATABASE_VERSION);
            super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
         }
}

MyActivity.java:

报告错误的行:

  // Initiate Database
            DatabaseHandler db = new DatabaseHandler(this); // "this" refer to the context

我感觉上下文导致问题,但我不确定上下文应该是什么。我刚从SQLiteOpenHelper

更改了

上一行有错误:

public DatabaseHandler(Context context) {
    //super(context, DATABASE_NAME, null, DATABASE_VERSION);
    super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
 }

1 个答案:

答案 0 :(得分:1)

Activity

中尝试这种方式
 DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 

 DatabaseHandler db = new DatabaseHandler(YourActivity.this);