我一直在尝试从assets文件夹中复制一个sqlite数据库文件,以便我创建一个sqlite数据库并在我的程序中使用它,这里是代码: -
首先是SqliteOpenHelperClass
public class DBHandler extends SQLiteOpenHelper {
protected static final String DATABASE_NAME_PRODUCTION = "productionComments.db";
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, DATABASE_VERSION);
}
//copy database from assets folder (.sqlite) file to an empty database
public void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open("prod.db");
// Path to the just created empty db
String outFileName = "/data/data/com.qarun.qpcbeta/databases/"+DATABASE_NAME_PRODUCTION;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
现在我应该调用copyDatabase()
方法的活动: -
public class ProductionCommentsActivity extends Activity {
private DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_production_comments);
dbHandler = new DBHandler(this, dbHandler.DATABASE_NAME_PRODUCTION, null, 1);
try {
dbHandler.copyDataBase();
//Toast.makeText(this,"Works",Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Log.d("copydb",e.getMessage());
//Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
}
}
以下是logcat中的错误: -
Process: com.qarun.qpcbeta, PID: 1271
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qarun.qpcbeta/com.qarun.qpcbeta.ProductionCommentsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
at com.qarun.qpcbeta.DBHandler.copyDataBase(DBHandler.java:100)
at com.qarun.qpcbeta.ProductionCommentsActivity.onCreate(ProductionCommentsActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我打开ProductionCommentsActivity