在android中使用本地数据库

时间:2014-03-20 15:46:05

标签: android eclipse sqlite

我正在尝试开发Android应用。我使用一个本地数据库“AndroidProject.db”,它放在assets文件夹中。当我尝试将数据库复制到正确的位置时,我遇到了NullPointer异常。我标记了发生异常的行。

源代码:

public class DBHelper extends SQLiteOpenHelper {
public static final int VERSION = 1;
public SQLiteDatabase myDatabase;
private static final String TAG = "DBHelper";
public Context myContext;
public static final String DB_NAME = "AndroidProject.db";

public DBHelper(Context context){
    super(context,DB_NAME,null,VERSION);
}

public DBHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

public void openDatabase() {
    importDatabase();
    Log.i(TAG, "open database");
}
    public void closeDatabase() {
    myDatabase.close();
    Log.i(TAG, "close database");
}

public void importDatabase() {
    File f = null;
    try {
        // Exception occurs here
        f = myContext.getDatabasePath(DB_NAME);
    } catch (NullPointerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (!f.exists()) {
        try {
            f.getParentFile().mkdirs();
            InputStream is = myContext.getAssets().open("AndroidProject.db");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();

            FileOutputStream fos = new FileOutputStream(f);
            fos.write(buffer);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            Log.e("File Error", e.getMessage());
        }
    }
    if (myDatabase == null || !myDatabase.isOpen()) {
        myDatabase = SQLiteDatabase.openDatabase(f.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
    }
}

public SQLiteDatabase getDatabase() {
    return myDatabase;
}


@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}


}

错误发生在此处:f = myContext.getDatabasePath(DB_NAME);

logcat的:

Caused by: java.lang.NullPointerException
at fu.android.example.travelvnproject_v01.Database.DBHelper.importDatabase(DBHelper.java:59)
at fu.android.example.travelvnproject_v01.Database.DBHelper.openDatabase(DBHelper.java:42)
at fu.android.example.travelvnproject_v01.Database.DBProvider.query(DBProvider.java:38)
at fu.android.example.travelvnproject_v01.LocalActivity.onCreate(LocalActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

1 个答案:

答案 0 :(得分:0)

由于myContext为null,因此发生错误。试试这个:

public DBHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);

    myContext = context;
}

修改

我使用我的建议更改对此源代码进行了测试,并且工作正常,为方便起见,我仅进行了一些其他更改,并删除了不必要的try / catch块:

public class DBHelper extends SQLiteOpenHelper {
    public static final int VERSION = 1;
    public SQLiteDatabase myDatabase;
    private static final String TAG = "DBHelper";
    public Context myContext;
    public static final String DB_NAME = "AndroidProject.db";

    public DBHelper(Context context){
        this(context, DB_NAME, null, VERSION); // Changed this to call the other constructor for convinience
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                    int version) {
        super(context, name, factory, version);

        myContext = context;
    }

    public void openDatabase() {
        importDatabase();
        Log.i(TAG, "open database");
    }
    public void closeDatabase() {
        myDatabase.close();
        Log.i(TAG, "close database");
    }

    public void importDatabase() {
        File f = myContext.getDatabasePath(DB_NAME);
        if (!f.exists()) {
            try {
                f.getParentFile().mkdirs();
                InputStream is = myContext.getAssets().open("AndroidProject.db");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();

                FileOutputStream fos = new FileOutputStream(f);
                fos.write(buffer);
                fos.flush();
                fos.close();
            } catch (Exception e) {
                Log.e("File Error", e.getMessage());
            }
        }
        if (myDatabase == null || !myDatabase.isOpen()) {
            myDatabase = SQLiteDatabase.openDatabase(f.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
        }
    }

    public SQLiteDatabase getDatabase() {
        return myDatabase;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}