Android:DB4O 问题

发布于 2024-12-19 01:42:50 字数 7079 浏览 6 评论 0原文

我正在尝试使用从 DB40 数据库返回的对象填充 listviewactivity 的列表,但我的应用程序不断崩溃,我不完全确定原因。

如果我知道如何阅读 LogCat 并能够根据它的内容进行理解,这将会有所帮助 -.- 我可以得到一个粗略的理解,我只是不知道到底出了什么问题。

public class AndrochieveActivity extends ListActivity { 
private List<Achievement> achieves;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ObjectContainer db = Db4oEmbedded.openFile("achievements.db4o");

    try{
        achieves = db.query(Achievement.class);
    } finally {
        db.close();
    }

    setListAdapter(new ArrayAdapter<Achievement>(this, R.layout.achievelist, achieves) {
        @Override
        public View getView(int pos, View cView, ViewGroup parent) {
            View row = mInflater.inflate(R.layout.achievelist, null);

            TextView title = (TextView) row.findViewById(R.id.title);
            title.setText(getItem(pos).GetTitle());

            TextView desc = (TextView)row.findViewById(R.id.desc);
            desc.setText(getItem(pos).GetDescription());

            TextProgressBar prog = (TextProgressBar)row.findViewById(R.id.prog);
            prog.setProgress(0);
            prog.setMax(getItem(pos).GetMax());

            return row;
        }
    });


}

我没有看到任何本质上会搞砸的事情。如果 Db4oEmbedded.openFile 不退出,则应该创建该文件,但我不断在 logcat 中收到一堆文件读取错误,如下所示:

11-29 14:21:44.359: D/AndroidRuntime(19396): Shutting down VM
11-29 14:21:44.359: W/dalvikvm(19396): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
11-29 14:21:44.389: E/AndroidRuntime(19396): FATAL EXCEPTION: main
11-29 14:21:44.389: E/AndroidRuntime(19396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.megalon.androchieve/com.megalon.androchieve.AndrochieveActivity}: com.db4o.ext.Db4oIOException: /achievements.db4o (Read-only file system)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1849)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1870)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1051)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.os.Looper.loop(Looper.java:150)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.main(ActivityThread.java:4312)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.lang.reflect.Method.invoke(Method.java:507)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at dalvik.system.NativeStart.main(Native Method)
11-29 14:21:44.389: E/AndroidRuntime(19396): Caused by: com.db4o.ext.Db4oIOException: /achievements.db4o (Read-only file system)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:26)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.FileStorage$FileBin.<init>(FileStorage.java:43)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.FileStorage.open(FileStorage.java:22)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.StorageDecorator.open(StorageDecorator.java:27)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.CachingStorage.open(CachingStorage.java:52)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.IoAdaptedObjectContainer.openImpl(IoAdaptedObjectContainer.java:57)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerBase$1.run(ObjectContainerBase.java:140)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.foundation.Environments.runWith(Environments.java:28)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerBase.withEnvironment(ObjectContainerBase.java:161)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerBase.open(ObjectContainerBase.java:131)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.IoAdaptedObjectContainer.<init>(IoAdaptedObjectContainer.java:35)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerFactory.openObjectContainer(ObjectContainerFactory.java:18)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:65)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:76)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.megalon.androchieve.AndrochieveActivity.onCreate(AndrochieveActivity.java:45)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1813)
11-29 14:21:44.389: E/AndroidRuntime(19396):    ... 11 more
11-29 14:21:44.389: E/AndroidRuntime(19396): Caused by: java.io.FileNotFoundException: /achievements.db4o (Read-only file system)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:132)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:173)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:19)
11-29 14:21:44.389: E/AndroidRuntime(19396):    ... 28 more
11-29 14:21:44.419: D/dalvikvm(19396): GC_CONCURRENT freed 308K, 49% free 2900K/5639K, external 0K/0K, paused 2ms+3ms
11-29 14:21:46.310: D/Process(19396): killProcess, pid=19396
11-29 14:21:46.310: D/Process(19396): dalvik.system.VMStack.getThreadStackTrace(Native Method)
11-29 14:21:46.320: D/Process(19396): java.lang.Thread.getStackTrace(Thread.java:745)
11-29 14:21:46.320: D/Process(19396): android.os.Process.killProcess(Process.java:797)
11-29 14:21:46.320: D/Process(19396): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
11-29 14:21:46.320: D/Process(19396): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854)

I'm trying to populate a listviewactivity's list with objects returned from an DB40 database, but my app keeps crashing and I'm not entirely sure why.

It would help if I knew how to read LogCat and be able to go from what it says -.- I can get a rough understanding I just don't know what exactly is going wrong.

public class AndrochieveActivity extends ListActivity { 
private List<Achievement> achieves;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ObjectContainer db = Db4oEmbedded.openFile("achievements.db4o");

    try{
        achieves = db.query(Achievement.class);
    } finally {
        db.close();
    }

    setListAdapter(new ArrayAdapter<Achievement>(this, R.layout.achievelist, achieves) {
        @Override
        public View getView(int pos, View cView, ViewGroup parent) {
            View row = mInflater.inflate(R.layout.achievelist, null);

            TextView title = (TextView) row.findViewById(R.id.title);
            title.setText(getItem(pos).GetTitle());

            TextView desc = (TextView)row.findViewById(R.id.desc);
            desc.setText(getItem(pos).GetDescription());

            TextProgressBar prog = (TextProgressBar)row.findViewById(R.id.prog);
            prog.setProgress(0);
            prog.setMax(getItem(pos).GetMax());

            return row;
        }
    });


}

I'm not seeing anything that would inherently screw it up. Db4oEmbedded.openFile should create the file if it doesn't exit, but I keep getting a bunch of file read errors in logcat, from what it looks like:

11-29 14:21:44.359: D/AndroidRuntime(19396): Shutting down VM
11-29 14:21:44.359: W/dalvikvm(19396): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
11-29 14:21:44.389: E/AndroidRuntime(19396): FATAL EXCEPTION: main
11-29 14:21:44.389: E/AndroidRuntime(19396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.megalon.androchieve/com.megalon.androchieve.AndrochieveActivity}: com.db4o.ext.Db4oIOException: /achievements.db4o (Read-only file system)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1849)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1870)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1051)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.os.Looper.loop(Looper.java:150)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.main(ActivityThread.java:4312)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.lang.reflect.Method.invoke(Method.java:507)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at dalvik.system.NativeStart.main(Native Method)
11-29 14:21:44.389: E/AndroidRuntime(19396): Caused by: com.db4o.ext.Db4oIOException: /achievements.db4o (Read-only file system)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:26)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.FileStorage$FileBin.<init>(FileStorage.java:43)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.FileStorage.open(FileStorage.java:22)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.StorageDecorator.open(StorageDecorator.java:27)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.CachingStorage.open(CachingStorage.java:52)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.IoAdaptedObjectContainer.openImpl(IoAdaptedObjectContainer.java:57)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerBase$1.run(ObjectContainerBase.java:140)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.foundation.Environments.runWith(Environments.java:28)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerBase.withEnvironment(ObjectContainerBase.java:161)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerBase.open(ObjectContainerBase.java:131)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.IoAdaptedObjectContainer.<init>(IoAdaptedObjectContainer.java:35)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.internal.ObjectContainerFactory.openObjectContainer(ObjectContainerFactory.java:18)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:65)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:76)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.megalon.androchieve.AndrochieveActivity.onCreate(AndrochieveActivity.java:45)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1813)
11-29 14:21:44.389: E/AndroidRuntime(19396):    ... 11 more
11-29 14:21:44.389: E/AndroidRuntime(19396): Caused by: java.io.FileNotFoundException: /achievements.db4o (Read-only file system)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:132)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:173)
11-29 14:21:44.389: E/AndroidRuntime(19396):    at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:19)
11-29 14:21:44.389: E/AndroidRuntime(19396):    ... 28 more
11-29 14:21:44.419: D/dalvikvm(19396): GC_CONCURRENT freed 308K, 49% free 2900K/5639K, external 0K/0K, paused 2ms+3ms
11-29 14:21:46.310: D/Process(19396): killProcess, pid=19396
11-29 14:21:46.310: D/Process(19396): dalvik.system.VMStack.getThreadStackTrace(Native Method)
11-29 14:21:46.320: D/Process(19396): java.lang.Thread.getStackTrace(Thread.java:745)
11-29 14:21:46.320: D/Process(19396): android.os.Process.killProcess(Process.java:797)
11-29 14:21:46.320: D/Process(19396): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
11-29 14:21:46.320: D/Process(19396): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

堇色安年 2024-12-26 01:42:50

正如异常所示,它试图在您无权写入访问的位置创建它。

如果文件变得很大,您可以使用 getExternalStorageDirectory() 下的文件以及 WRITE_EXTERNAL_STORAGE 权限。

您应该能够使用上下文和 ctx.getDir("data", 0) 创建应用程序数据目录的完整路径,将文件名附加到该路径,然后将其交给 db4o。

It's trying to create it in a place that you don't have write access to, as the exception says.

You could use a file under getExternalStorageDirectory() if it's going to get large, along with the WRITE_EXTERNAL_STORAGE permission.

You should be able to create a full path to your app's data directory using the context and ctx.getDir("data", 0), append the filename to that, and hand that off to db4o.

孤芳又自赏 2024-12-26 01:42:50

看看这段代码(对于 sqlite)

            String currentTimeString = new SimpleDateFormat(
                    "yyyy.MM.dd.'at'.hhaaa").format(new Date());
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(Environment.getExternalStorageDirectory(),
                    currentTimeString + ".jpg");
            outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, TAKE_PICTURE);

看看我的新文件语句如何给出特定目录,您也需要这样做。
也看看这里,因为它是完全相同的问题 尝试读取 ASP.NET 项目中 App_Data 中的文件时访问被拒绝 - 有任何指示吗?
如果这些都没有帮助,您需要发布 Db4oEmbedded 的代码,

哎呀,没有意识到该帖子是 asp.net 哦,如果这些都不起作用,就像我说的那样,发布其他类的代码

look at this code (for sqlite)

            String currentTimeString = new SimpleDateFormat(
                    "yyyy.MM.dd.'at'.hhaaa").format(new Date());
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(Environment.getExternalStorageDirectory(),
                    currentTimeString + ".jpg");
            outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, TAKE_PICTURE);

see how my new file statement gives the specific directory, you need to do this as well.
also look here as it is exactly the same problem Getting Access denied trying to read a file in App_Data in a ASP.NET project - Any pointers?
if none of this helps you need to post the code for Db4oEmbedded

whooops didnt realize that post was asp.net o well if none of this works like i said post the code for that other class

轻拂→两袖风尘 2024-12-26 01:42:50

您面临的问题是由于 ObjectContainer db = Db4oEmbedded.openFile("achievements.db4o"); 造成的。

  1. 您正在尝试在您没有权限的根目录中创建数据库文件。因此,在 applicationContext 中创建,应用程序可以像其他数据库(SQLite)一样轻松访问数据库,即在 data/data/YOUR_APPLIACTION_PACKAGE_NAME/DATABASENAME 中创建。

  2. 第二件事是不要尝试在 UIThread 中打开、创建数据库文件,因为它会给出 NetworkOnMainThreadException。让它在后台运行。

有关示例代码,您可以参考此处:https://github.com/raghav1sharma/BlogSamples

The problem you are facing is due to ObjectContainer db = Db4oEmbedded.openFile("achievements.db4o");.

  1. You are trying to create the database file in root directory in which you do not have permission. So create in applicationContext where database can be easily accessible to the application like other databases(SQLite) do i.e create in data/data/YOUR_APPLIACTION_PACKAGE_NAME/DATABASENAME.

  2. Second Thing is do not try to open,create database file in UIThread as it will give NetworkOnMainThreadException. Let it be in background.

For sample code you can refer here :https://github.com/raghav1sharma/BlogSamples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文