3

In my SQLite setup class I have a DbHelper that requires a Context as a part of it. To Setup the context, I just use a constructor in my SQLite class that requires a context as a part of it parameters.

But I just encountered a problem. When trying to call my SQLite class from a class which is not an Activity, I can't use as the context classname.this, and it is bugging me.

I also tried to do this to declare a context:

protected Context context;

And then later on call it like this:

SetSql PlayerObject = new SetSql(This.context);

But this didn't work either.

Any suggestion please?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rel-s
  • 6,108
  • 11
  • 38
  • 50

2 Answers2

6

pass application Context like this

SetSql PlayerObject = new SetSql(this.getApplicationContext());

now it should work fine.

Ravi1187342
  • 1,247
  • 7
  • 14
  • I dont think it will work since my class is not an Activity as i mentioned, and it is not Setup in the manifest file. I tried it and it says the method getApplicationContext() is undefined for RandPlayers (my class). If i need to, how can i set it up in the manifest file? – rel-s Apr 13 '12 at 08:55
  • 1
    make a static global variable `static Context appContext;` and assign the application context in `appContext`. `appContext=getApplicationContext();` in `main activity's` `onCreate()` method. now access it in your non-activity class. `new SetSql(MainActivity.appContext);` – Ravi1187342 Apr 13 '12 at 09:28
  • By main activity, do you mean the one that first launches? that will be my splash activity. Is that what you mean? – rel-s Apr 13 '12 at 09:37
  • yes you can make a static global Context variable in splash activity and store ApplicationContext in that variable. – Ravi1187342 Apr 13 '12 at 09:43
  • ha you beautiful creature. This method might cause a memory leak if used incorrectly, but still a good workaround. – vergil corleone Jun 23 '13 at 21:31
-1
public class DBHelper extends SQLiteOpenHelper {

private static final String TAG = "DBHelper";
private static final int DB_VERSION = 1;

private Context context;
private String DB_PATH;
private String DB_NAME;
private String TABLE_NAME;
private boolean booCreate;
private String createString;
private SQLiteDatabase myDataBase;

public DBHelper(Context context, String db_name, String table_name,
        boolean booCreate, String createString) {
    super(context, db_name, null, DB_VERSION);
    this.context = context;
    this.DB_NAME = db_name;
    this.DB_PATH = "/data/data/" + FindPackageName() + "/databases/";
    Log.d(TAG, "DBPATH=" + DB_PATH);
    this.TABLE_NAME = table_name;
    this.createString = createString;
    this.booCreate = booCreate;
    Log.d(TAG,"booCreate=" + String.valueOf(this.booCreate));
    try {
        createDataBase();
    } catch (IOException e) {
        throw new Error("Unable to Create Database");
    }
}

and in your activity

dbHelper = new DBHelper(this, this.getString(R.string.db_name_contact),
            this.getString(R.string.table_name_contacts), true, createString);

Watch this video and download code from Part-III of this tutorial

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • Im afraid this code will change some things in my application that i can't change. Do you happen to know an easy fix for the Context problem? – rel-s Apr 13 '12 at 08:56