2012-11-08 72 views
0

我想使用單例類來創建這個數據庫,因爲我需要它爲應用程序中的每個活動打開。我一直從任一「getFilesDir」得到一個空指針錯誤或「如果(例如== NULL)調用,並且因爲我不是那麼熟悉了Android編程我不知道是怎麼回事。設置db4o數據庫的麻煩

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

方式這是有條理的。

  • Java類是總是命名與適當的情況下 - 所以它應該是「AnotherTest」。
  • 你實際上並沒有創建一個單例,因爲你的實例和getInstance()都不是靜態的。此外,你永遠不會通過「getInstance()」訪問你的實例。
  • 你有一個AnotherTest類,它有一個AnotherTest的實例,它永遠不會被初始化,並且可以在AnotherTest的構造函數中訪問。這只是保證爲空。
  • 你所依賴的應用程序超類,這是永遠,永遠提供,因爲應用程序應該在AndroidManifest.xml,在那裏將建設爲你聲明的隱含狀態。同時,你在你的事件處理程序手動構造一個實例,所以它有沒有你所期望的狀態 - 但無論如何,它會摔倒在自己的構造,因爲它會嘗試一個空指針引用本身,如上所述。

你的問題與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); 
+0

在偶數處理程序使用該調用的唯一麻煩的是,我們得到了一個靜態參考從活動中使用getApplicationContext()時出現非靜態方法錯誤。 – user1799107

+0

能夠使用 'AnotherTest.getInstance(getBaseContext())storePlay(新野);' 而截至目前,從一些小的快速測試我做的工作。再次,非常感謝幫助和解釋發生了什麼問題,我非常感謝! – user1799107