Nexion
Nexion

Reputation: 433

Android: DB4O Problems

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)

Upvotes: 0

Views: 1277

Answers (4)

Raghav Sharma
Raghav Sharma

Reputation: 800

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

Upvotes: 0

owen gerig
owen gerig

Reputation: 6172

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

Upvotes: 0

GrimmRanger
GrimmRanger

Reputation: 428

It appears that Achievement.class may not be set up with the database properly, according to the following:

http://tracker.db4o.com/browse/COR-2005;jsessionid=B1CD509911380518AC540A6E75F41529?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aworklog-tabpanel

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160241

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.

Upvotes: 1

Related Questions