这是我的DBhelper类。我想将我的数据库从assest文件夹复制到我当前的DB Path.I我在CopyDatabase函数的InputStream中收到错误。请帮助。
package com.example.dbtest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
public class DBhelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.example.dbtest/databases/";
private static String DB_NAME = "photodb";
private SQLiteDatabase myDataBase;
private final Context myContext;
File dbFile;
File dbFilepath;
public DBhelper(Context context) {
super(context, DB_NAME, null, 1);
String state=Environment.getExternalStorageState();
System.out.println("My Storage State:-"+state);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
System.out.println("Ufffff");
String path = dbFile.getAbsolutePath();
System.out.println("My database Path:--"+path);
}
if(!dbExist)
{
dbFile= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Dbtest");
boolean flag=dbFile.mkdir();
dbFilepath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Dbtest"+" /"+"photodb");
dbFilepath.createNewFile();
System.out.println("Ufffff naaa..File hoeche?..."+flag);
try {
copyDataBase(myContext);
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public boolean checkDataBase()
{
dbFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Dbtest"+" /"+"photodb");
return dbFile.exists();
}
private void copyDataBase(Context cont) throws IOException{
InputStream myInput = cont.getAssets().open(DB_NAME);
String outFileName = dbFilepath.getAbsolutePath();
OutputStream myOutput = new FileOutputStream(outFileName);
//OutputStream myOutput1 = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = dbFile.getAbsolutePath();
System.out.println("MY DATABASE PATH:----"+myPath);
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void add(String imagepath,String description,String location)
{
String sql = "insert into photodetails values(null,'hello','bolo','aache');";
myDataBase.execSQL(sql);
}
}
我的主要活动
public class MainActivity extends Activity {
DBhelper mydb;
Context cont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBhelper(cont);
try {
mydb.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mydb.openDataBase();
mydb.add("helloworld", "Hoeche", "MGroad");
// mydb.openDataBase();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的Logcat错误是
09-03 11:48:40.342: E/AndroidRuntime(27139): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbtest/com.example.dbtest.MainActivity}: java.lang.NullPointerException
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.access$700(ActivityThread.java:140)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.os.Looper.loop(Looper.java:137)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.main(ActivityThread.java:4921)
09-03 11:48:40.342: E/AndroidRuntime(27139): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 11:48:40.342: E/AndroidRuntime(27139): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-03 11:48:40.342: E/AndroidRuntime(27139): at dalvik.system.NativeStart.main(Native Method)
09-03 11:48:40.342: E/AndroidRuntime(27139): Caused by: java.lang.NullPointerException
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.example.dbtest.DBhelper.copyDataBase(DBhelper.java:98)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.example.dbtest.DBhelper.createDataBase(DBhelper.java:77)
09-03 11:48:40.342: E/AndroidRuntime(27139): at com.example.dbtest.MainActivity.onCreate(MainActivity.java:25)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.Activity.performCreate(Activity.java:5206)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-03 11:48:40.342: E/AndroidRuntime(27139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-03 11:48:40.342: E/AndroidRuntime(27139): ... 11 more
更新
即使我改变了行并写了
AssetManager myassest = cont.getAssets();
InputStream myInput = myassest.open("photodb");
它在cont.getAssests()中给出了错误;它说NULL指针异常
答案 0 :(得分:1)
此错误是由于您将Null上下文cont
传递给DBhelper
类,但cont
未初始化。将您的onCreate()
更改为
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBhelper(MainActivity.this);
try {
mydb.createDataBase();
} catch (IOException e) {
e.printStackTrace();
Log.e("MainActivity", "Got IOException = " + e.getMessage());
}
mydb.openDataBase();
mydb.add("helloworld", "Hoeche", "MGroad");
// mydb.openDataBase();
}
使用扩展名为(photodb.db or photodb.sqlite)
的资产中的同名db,而不是
private static String DB_NAME = "photodb";
答案 1 :(得分:0)
尝试这个
public final static String APP_PATH_SD_CARD = "/database";
private static final String DATABASE_NAME = "photodb.db";
private boolean checkDataBase(){
boolean checkdb = false;
try{
String myPath = context.getFilesDir().getAbsolutePath().replace("files", "databases")+File.separator + DATABASE_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}