Android和db4o.Can在模拟器上创建db,但在真实设备上不创建

时间:2011-11-01 13:51:07

标签: android db4o

我正在编写我的第一个Android应用。我想使用db4o,因为我之前从未使用过它。我只是尝试打开/创建数据库。当我在虚拟设备上运行时,一切正常。但是,当我去我的真实设备时,我总是得到下面写的例外:

  ...ActivityClass{
private void deleteDB() {
    DatabaseHandler db = new DatabaseHandler();
    db.open(getString(R.string.database));
    db.deleteAll();
    db.close();
}
}....
The R.string.database = /data/data/de.workingtimemanager.urb/databases/WTM.db
my package = de.workingtimemanager.urb

public class DatabaseHandler {
    private ObjectContainer database;
public DatabaseHandler() {}

public void open(String name) {
    EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();
    conf.common().objectClass(User.class).cascadeOnDelete(true);
    conf.common().objectClass(User.class).cascadeOnUpdate(true);
    conf.common().objectClass(Project.class).cascadeOnDelete(true);
    conf.common().objectClass(Project.class).cascadeOnUpdate(true);
    database = Db4oEmbedded.openFile(conf, name);
    }
}
11-01 13:38:36.385: ERROR/AndroidRuntime(4774): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.workingtimemanager.urb/de.workingtimemanager.urb.WorkingTimeManagerActivity}: com.db4o.ext.Db4oIOException: /data/data/de.workingtimemanager.urb/databases/WTM.db (No such file or directory)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.os.Handler.dispatchMessage(Handler.java:99)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.os.Looper.loop(Looper.java:123)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.ActivityThread.main(ActivityThread.java:3691)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at java.lang.reflect.Method.invokeNative(Native Method)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at java.lang.reflect.Method.invoke(Method.java:507)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at dalvik.system.NativeStart.main(Native Method)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774): Caused by: com.db4o.ext.Db4oIOException: /data/data/de.workingtimemanager.urb/databases/WTM.db (No such file or directory)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:26)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.io.FileStorage$FileBin.<init>(FileStorage.java:43)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.io.FileStorage.open(FileStorage.java:22)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.io.StorageDecorator.open(StorageDecorator.java:27)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.io.CachingStorage.open(CachingStorage.java:52)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.internal.IoAdaptedObjectContainer.openImpl(IoAdaptedObjectContainer.java:57)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.internal.ObjectContainerBase$1.run(ObjectContainerBase.java:140)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.foundation.Environments.runWith(Environments.java:28)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.internal.ObjectContainerBase.withEnvironment(ObjectContainerBase.java:161)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.internal.ObjectContainerBase.open(ObjectContainerBase.java:131)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.internal.IoAdaptedObjectContainer.<init>(IoAdaptedObjectContainer.java:35)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.internal.ObjectContainerFactory.openObjectContainer(ObjectContainerFactory.java:18)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:65)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at de.workingtimemanager.urb.db.DatabaseHandler.open(DatabaseHandler.java:33)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at de.workingtimemanager.urb.WorkingTimeManagerActivity.deleteDB(WorkingTimeManagerActivity.java:22)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at de.workingtimemanager.urb.WorkingTimeManagerActivity.onCreate(WorkingTimeManagerActivity.java:15)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     ... 11 more

11-01 13:38:36.385: ERROR/AndroidRuntime(4774): Caused by: java.io.FileNotFoundException: /data/data/de.workingtimemanager.urb/databases/WTM.db (No such file or directory)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at java.io.RandomAccessFile.<init>(RandomAccessFile.java:132)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at java.io.RandomAccessFile.<init>(RandomAccessFile.java:173)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:19)

11-01 13:38:36.385: ERROR/AndroidRuntime(4774):     ... 29 more

2 个答案:

答案 0 :(得分:2)

您是否检查过可以在该位置创建和编写文件?通常,您使用数据目录来确保您拥有适当的权限:

    String filePath = this.getFilesDir() + "/android.db4o";
    final EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
    config.common().add(new AndroidSupport());
    ObjectContainer db = Db4oEmbedded.openFile(config,filePath);

答案 1 :(得分:0)

检查库中是否存在db4o jar文件。如果Library也有AndroidDependency,它还应该包含db4o jar文件。如果它不存在,则可能会出现ClassNotFoundError

为了解决此问题,您可以从项目库中删除AndroidDependency或将db4o jar文件添加到AndroidDependency