2012-08-09 47 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