2012-01-16 19 views
0

我收到「應用程序沒有關閉在這裏打開遊標或數據庫對象」的錯誤,所以我決定的意圖,而不是關閉/打開數據庫之間傳遞datahelper每一個意圖..Android的傳球datahelper

我試圖把在附加功能,但沒有工作...

感謝您的幫助

this.dhn =新DataHelper(本);

public void StartGame(View v) { 
Intent intent = new Intent(StartScreen.this, Game.class); 
intent.putExtra("dhn",this.dhn); 
startActivity(intent); 
} 

回答

0

我改變了我的DataHelper獨居,看上去能夠從任何其他意圖沒有新的輔助使用..看起來現在的工作。要測試更多

private static DataHelper singleton; 

    public static DataHelper getDataHelper(Context context) { 
      if (singleton == null) { 
        singleton = new DataHelper(context); 
        OpenHelper openHelper = new OpenHelper(singleton.context); 
        singleton.db = openHelper.getWritableDatabase(); 
      } 
      if(!singleton.db.isOpen()){ 
        OpenHelper openHelper = new OpenHelper(singleton.context); 
        singleton.db = openHelper.getWritableDatabase(); 
      } 
      singleton.context = context; 
      return singleton; 
    } 

    private DataHelper(Context context) { 
     this.context = context; 
} 
0

這是我如何處理在我的代碼的問題:

public class DataOpenHelper extends SQLiteOpenHelper 
{ 
    private static Object lock = new Object(); 
    private static DataOpenHelper inst = null; 
    private static Context nextContext = null; 

    private DataOpenHelper() 
    { 
     super(nextContext); 
     nextContext = null; 
    } 

    public static void registerContext(Context ctx) 
    { 
     synchronized (lock) 
     { 
      nextContext = ctx; 
     } 
    } 

    public static DataOpenHelper getInstance() throws ContextNotRegisteredException 
    { 
     if (inst == null) 
     { 
      synchronized (lock) 
      { 
       if (inst == null) 
       { 
        if (nextContext == null) 
        { 
         throw new ContextNotRegisteredException(); 
        } 

        inst = new DataOpenHelper(); 
       } 
      } 
     } 
    } 

} 

    public class ContextNotRegisteredException extends Exception{};