Max Usanin
Max Usanin

Reputation: 2499

trying to connect to the database, context = null

Trying to connect to the database ; I have a problem context = null (may well must to be). I can not understand what the problem

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();


        }
    }

Upvotes: 0

Views: 128

Answers (3)

Praveenkumar
Praveenkumar

Reputation: 24476

Try this -

@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();
}

or you can give like -

    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();

Upvotes: 1

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

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

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

Upvotes: 1

Fei
Fei

Reputation: 1996

Try LBD conection = LBD.get(this);

Upvotes: 0

Related Questions