2012-08-09 73 views
0

試圖連接到數據庫; 我有一個問題context = null(可能必須是)。我不明白有什麼問題試圖連接到數據庫,context = null

import java.util.HashMap; 

    import android.app.Activity; 

    import android.content.Context; 
    import android.os.Bundle; 

    public class DatabaseTable extends Activity { 

     private Context context; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.database_table); 



      LBD conection = LBD.get(context); // (context = null) ??? 
      Settings setting = new Settings(conection.getSQLiteDatabase()); 
      setting.create(); 


     } 
    } 
+0

嘗試DatabaseTable.this – Akshay 2012-08-09 13:32:04

+0

給'context = DatabaseTable.th在'setcontentView()' – Praveenkumar 2012-08-09 13:33:47

回答

1

試試這個 -

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.database_table); 

    context = DatabaseTable.this; // you can give this instead of DatabaseTable.this also. 

    LBD conection = LBD.get(context); // (context = null) ??? 
    Settings setting = new Settings(conection.getSQLiteDatabase()); 
    setting.create(); 
} 

,或者你可以給喜歡 -

LBD conection = LBD.get(DatabaseTable.this); // from this you don't need to Create any context instance of Context class. Directly pass the context here. 
    Settings setting = new Settings(conection.getSQLiteDatabase()); 
    setting.create(); 
0

嘗試LBD conection = LBD.get(this);

1
context=this;//you forgot this... 

LBD conection = LBD.get(context); // (context = null) ??? 
Settings setting = new Settings(conection.getSQLiteDatabase()); 
setting.create(); 
+0

好,但問題未解決 – 2012-08-09 13:48:35

+0

db =(new OpenHelper(context))。getWritableDatabase()= nullpointerexception and context不再爲null – 2012-08-09 13:51:29