0

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);
    }
Jay
  • 41
  • 1
  • 7
  • because that doesn't return to any context or that context is not pointing to the content where your method is, try `class.this` or `application.getContext()` or `getApplicationContext()` whatever is available that will result to actually point the context where your method is. http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and – Roljhon Mar 04 '17 at 16:53
  • thought I was following a good tutorial... :-(( should I rebuild my code? – Jay Mar 04 '17 at 16:55
  • 1
    Question is unclear... Please show a [mcve] of all relevant code. Meaning the class definition of where you are calling that context method – OneCricketeer Mar 04 '17 at 16:56
  • I doubt your tutorial stores `DataConn mDbHelper;` within the `DataConn` class itself – OneCricketeer Mar 04 '17 at 16:57
  • @Jay that I wouldn't know, your question is very minimal as what cricket above mentioned, if you could provide a more detailed question, that might help – Roljhon Mar 04 '17 at 16:57
  • I already updated the code in my question. please refer above – Jay Mar 04 '17 at 17:03
  • @cricket_007 damn it is... :-(( – Jay Mar 04 '17 at 17:16
  • We can't answer your question without seeing what type of class `ClearRecentPlayer()` is defined it. That will tell us (and you) why you can't call `getContext()` – OneCricketeer Mar 04 '17 at 18:06

3 Answers3

3

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)

Community
  • 1
  • 1
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
  • 1
    I cant. I can only use 'getContext()' in this class.. I dont know why – Jay Mar 04 '17 at 17:39
  • are you trying this in an activity? – Arun Shankar Mar 07 '17 at 06:43
  • 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)` – Arun Shankar Mar 07 '17 at 06:44
2

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);
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I think I got the answer. I changed the 'DataConn mDHelper;' to just 'this' and use 'getApplicationContext()' instead of 'getContext()' in different java class. Thank you @cricket_007 – Jay Mar 05 '17 at 09:23
  • but the problem now is that the cursor is returning null. :-(( – Jay Mar 05 '17 at 13:50
  • please help me. X-(( – Jay Mar 05 '17 at 13:50
  • Help you with what? You don't need `getApplicationContext()` here. If you have a new problem related to this code, please post a new question with a [mcve] – OneCricketeer Mar 05 '17 at 14:40
1

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

Mr. Mad
  • 1,230
  • 1
  • 14
  • 28