我正在尝试将sqlite数据库导入我的Android应用程序。我在这里看到了另一个问题的解决方案代码。
我试图这样做,但却出现 unfortunately application has stopped
错误。
我该如何解决?
以下是logcats:
03-09 13:05:14.932: E/AndroidRuntime(1092): FATAL EXCEPTION: main
03-09 13:05:14.932: E/AndroidRuntime(1092): java.lang.Error: ErrorCopyingDataBase
03-09 13:05:14.932: E/AndroidRuntime(1092): at com.nasozby.marifetname.Db.createDataBase(Db.java:48)
03-09 13:05:14.932: E/AndroidRuntime(1092): at com.nasozby.marifetname.Adapter.createDatabase(Adapter.java:28)
03-09 13:05:14.932: E/AndroidRuntime(1092): at com.nasozby.marifetname.MainActivity.onCreate(MainActivity.java:29)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.Activity.performCreate(Activity.java:5104)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.os.Looper.loop(Looper.java:137)
03-09 13:05:14.932: E/AndroidRuntime(1092): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-09 13:05:14.932: E/AndroidRuntime(1092): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:05:14.932: E/AndroidRuntime(1092): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 13:05:14.932: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-09 13:05:14.932: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-09 13:05:14.932: E/AndroidRuntime(1092): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果您尝试使用您的应用程序发送初始数据库文件,则需要进入“assets”目录并在开始时复制(如果它尚不存在)。
这是我过去处理此问题的一种简单方法,此代码是我SQLiteOpenHelper
的一部分(在ctor中触发createDatabase
):
private void createDatabase() {
if (!databaseExists()) {
Log.i(TAG, "OpenHelper createDatabase, database does NOT exist, will be copied/created...");
try {
this.copyDatabase();
} catch (IOException e) {
Log.e(TAG, "OpenHelper error copying database " + e.getMessage(), e);
}
} else {
Log.d(TAG, "OpenHelper createDatabase, database exists, leaving it alone");
}
}
private void copyDatabase() throws IOException {
InputStream is = context.getAssets().open(DB_NAME + ".sqlite");
File dbFile = context.getDatabasePath(DB_NAME);
dbFile.getParentFile().mkdirs();
Log.i(TAG, "OpenHelper copyDatabase creating file at path:" + dbFile.getAbsolutePath());
OutputStream os = new FileOutputStream(dbFile.getAbsolutePath());
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
}
private boolean databaseExists() {
File dbFile = context.getDatabasePath(DB_NAME);
return dbFile.exists();
}