设置db4o数据库时遇到问题

时间:2012-11-08 04:54:35

标签: android db4o

我正在尝试使用单例类来创建此数据库,因为我需要为应用程序中的每个活动打开它。我一直从“getFilesDir”或“if(instance == null)调用中得到一个空指针错误,因为我不熟悉android编程,所以我不确定是怎么回事。”

package com.database;

import java.io.File;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

import com.businessclasses.Field;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.AndroidSupport;
import com.db4o.config.EmbeddedConfiguration;

public final class anotherTest extends Application {
    private static ObjectContainer playsDB;
    private static ObjectContainer gamePlanDB;

    private anotherTest instance;

    public anotherTest(){

        final EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
        config.common().add(new AndroidSupport());
        //instance.getFilesDir().getPath()
        new File(getFilesDir().getPath(), "/PlaysDB.db4o").delete();

        Log.d("db", "" + instance.getFilesDir().getPath());
        //getFilesDir().getPath();
        //String appPath = "/data/data/PlaysDB/database";

        playsDB = Db4oEmbedded.openFile(config, getFilesDir().getPath() +     "/PlaysDB.db4o");
        //File test = new File(appPath + "/PlaysDB.db4o");
        //if(test != null){
        //  Log.d("db", "file was created");
        //  Log.d("db", "path >> " + test.getAbsolutePath());
            //test.delete();
        //}

        //playsDB = Db4oEmbedded.openFile(config, appPath + "/PlaysDB.db4o");
    }

    public synchronized anotherTest getInstance(){
        if(instance == null){
            instance = new anotherTest();
        }
        return instance;
    }

    public void storePlay(Field field){
        if(field != null){
            if(playsDB == null){
                Log.d("db", "database is null");
            }
            playsDB.store(field);
            playsDB.commit();
            Log.d("added play", "play added to db");    
        }
        else{
            Log.e("field input null", "play not added to db");
        }
    }   
}

以下是我在其中一项活动中的电话。

   public void onClick(View v) {
        String formation = playFormation.getText().toString();
        String name = playName.getText().toString();
        String type = playType.getSelectedItem().toString();
        //TODO:send to database

        Field newField = new Field();
        newField.setPlayName(name);
        newField.setPlayType(type);

        anotherTest temp = new anotherTest();
        temp.getInstance().storePlay(newField);
    }

过去几周一直试图使这个数据库工作无济于事。任何帮助或指导将不胜感激。

1 个答案:

答案 0 :(得分:1)

这种结构的方式没有意义。

  • Java类始终以适当的大小写命名 - 因此它应该是“AnotherTest”。
  • 您实际上并没有创建单例,因为您的实例和getInstance()都不是静态的。此外,您永远不会通过“getInstance()”访问您的实例。
  • 你有一个类AnotherTest,它有一个字段作为AnotherTest的实例,它永远不会被初始化,并且可以在AnotherTest的构造函数中访问。这只能保证为空。
  • 您依赖于Application超类的隐式状态,它永远不会被提供,因为Application应该在androidmanifest.xml中声明,它将在那里为您构造。同时,您正在事件处理程序中手动构造一个实例,因此它没有您期望的状态 - 但无论如何,它将在它自己的构造函数中失败,因为它试图引用一个指向自身的空指针,如上所述。 / LI>

你的问题与DB40无关,而且与不理解你正在做的事情有关。

需要更像是:

public final class AnotherTest {


    private static AnotherTest instance;

    private final Context context;

    private AnotherTest(Context context){
        this.context = context;

        // Do whatever you need to do with DB4O using *only* the supplied context here
    }

    public static synchronized AnotherTest getInstance(Context context){
        if(instance == null){
            instance = new AnotherTest(context);
        }
        return instance;
    }
}

在事件处理程序中:

AnotherTest.getInstance(Context.getApplicationContext()).storePlay(newField);