只是一個簡單的錯誤,但我真的很難試圖解決這個問題。爲什麼這個getContext()
不適用?Android:getContext()error
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);
}
以及與此...
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);
}
因爲不返回任何背景或上下文不指向內容在您的方法是,請嘗試使用'class.this'或'application.getContext()'或'getApplicationContext()',這將導致實際將上下文指向您的方法所在的位置。 http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and – Roljhon
以爲我在跟着一個很好的教程...: - ((我應該重建我的代碼嗎? – Jay
問題不清楚.. 。請顯示所有相關代碼的[mcve]。這意味着你在哪裏調用該上下文方法的類定義 –