Jay
Jay

Reputation: 41

Android: getContext() error

just a simple error I have but I am really having a hard time trying to solve this problem. why is this getContext() are not applied?

 public void ClearRecentPlayer() {

        mDbHelper = new DataConn(getContext()); //<---getContext() in redline(not applied)
        SQLiteDatabase db = mDbHelper.getWritableDatabase();

        ContentValues v = new ContentValues();
        v.put(FeedReaderContract.FeedEntry.COLUMN_NAME_STATS, 0);

        String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_STATS + " = ?";
        String[] selectionArgs = { "0" };
        int c = db.update(
                FeedReaderContract.FeedEntry.TABLE_NAME_PLAYER,
                v,
                selection,
                selectionArgs);
    }

and with this...

public class DataConn extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "db_egame.db";

    DataConn mDbHelper;

    public DataConn(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_EASY_ENTRIES);
        db.execSQL(SQL_CREATE_HARD_ENTRIES);
        db.execSQL(SQL_CREATE_DIFF_ENTRIES);
        db.execSQL(SQL_CREATE_PLAYER_ENTRIES);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_EASY_ENTRIES);
        db.execSQL(SQL_DELETE_HARD_ENTRIES);
        db.execSQL(SQL_DELETE_DIFF_ENTRIES);
        db.execSQL(SQL_DELETE_PLAYER_ENTRIES);
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
        onCreate(db);
    }

Upvotes: 0

Views: 7213

Answers (3)

Arun Shankar
Arun Shankar

Reputation: 2593

As explained here (https://stackoverflow.com/a/10641257/2319627)

View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.

Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext.

So, it will be better to use getApplicationContext() when you are trying to use a DataBaseHelper.

And, you can call getApplicationContext from activity or service only, or from an instance of context. Like activity.getApplicationContext()

You need an application context for a Database helper class. So, pass a context to the database on initialization

ClearRecentPlayer method is in an activity? else, you have to pass the application context to the class from which you call ClearRecentPlayer method.

you can either create a member variable .Context in that class, or you can call the ClearRecentPlayer method as ClearRecentPlayer (Context context)

Upvotes: 3

OneCricketeer
OneCricketeer

Reputation: 191963

getContext() is only an available method of a View.

If your method is in that database class, you don't actually need the Context. Or any instance of DataConn within its own class.

public class DataConn extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "db_egame.db";

    private Context mContext;

    public DataConn(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = context;
    }


    public void clearRecentPlayer() {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues v = new ContentValues();
        v.put(FeedReaderContract.FeedEntry.COLUMN_NAME_STATS, 0);

        String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_STATS + " = ?";
        String[] selectionArgs = { "0" };
        int c = db.update(
            FeedReaderContract.FeedEntry.TABLE_NAME_PLAYER,
            v,
            selection,
            selectionArgs);
    }

Upvotes: 2

Mr. Mad
Mr. Mad

Reputation: 1238

Try getApplicationContext() instead of getContext() for activity/AppCompactActivity,

Upvotes: 1

Related Questions