0

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


        }
    }
4

3 回答 3

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

LBD conection = LBD.get(context);  // (context = null) ???
Settings setting = new Settings(conection.getSQLiteDatabase());
setting.create();
于 2012-08-09T13:33:19.517 回答
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();
于 2012-08-09T13:36:02.433 回答
0

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

于 2012-08-09T13:32:27.693 回答