尝试连接数据库,context = null

时间:2012-08-09 13:30:45

标签: android sqlite android-context

尝试连接数据库; 我有一个问题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();


        }
    }

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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();

答案 2 :(得分:0)

尝试LBD conection = LBD.get(this);