I'm writing an Android app and am getting this error, but I'm not sure why. Can someone help me understand why I'm getting this error?
Cannot make a static reference to the non-static method updateScores(List<Score>) from the type DatabaseHandler
Here's the relevant code.
public class ScoreList extends SherlockFragmentActivity {
List<Score> listScore = new ArrayList<Score>();
public void updateListView() {
listViewScore.setAdapter(new ScoreListAdapter(ctx,
R.layout.score_row_item, listScore));
DatabaseHandler.updateScores(listScore);
}
}
Here's the DatabaseHandler class. I tried making the function static, but it won't work that way due to errors.
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "scoreKeeper";
private static final String TABLE_GAMES = "games";
private static final String KEY_NAME = "name";
private static final String KEY_CHANGE = "scoreChange";
private static final String KEY_TOTAL = "scoreTotal";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_GAMES_TABLE = "CREATE TABLE " + TABLE_GAMES + "("
+ KEY_NAME + " INTEGER PRIMARY KEY," + KEY_CHANGE + " TEXT,"
+ KEY_TOTAL + " TEXT" + ")";
db.execSQL(CREATE_GAMES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GAMES);
onCreate(db);
}
public void addScore(Score score) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, score.getName());
values.put(KEY_CHANGE, score.getScoreChange());
values.put(KEY_TOTAL, score.getScoreTotal());
// Inserting Row
db.insert(TABLE_GAMES, null, values);
db.close();
}
public List<Score> getAllScores() {
List<Score> scoreList = new ArrayList<Score>();
String selectQuery = "SELECT * FROM " + TABLE_GAMES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Score score = new Score("","","");
score.setName(cursor.getString(0));
score.setScoreChange(cursor.getString(1));
score.setScoreTotal(cursor.getString(2));
// Adding contact to list
scoreList.add(score);
} while (cursor.moveToNext());
}
return scoreList;
}
public void updateScores(List<Score> score) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GAMES);
onCreate(db);
ContentValues values = new ContentValues();
for(int i = 0; i < score.size(); i++){
values.put(KEY_NAME, score.get(i).getName());
values.put(KEY_CHANGE, score.get(i).getScoreChange());
values.put(KEY_TOTAL, score.get(i).getScoreTotal());
}
}
}
public void updateScores(List<Score> score)
change to
public static void updateScores(List<Score> score)
Why?
Because you are using static call
DatabaseHandler.updateScores(listScore)
Because you are accessing the method with static reference
DatabaseHandler.updateScores(listScore);
means with class name..
You have to make a instance of DatabaseHandler
class and use that method.
Or make a updateScores
as static method like,
static public void updateScores()
But I have a doubt you have more codes in DatabaseHandler
Class. (As its sqlite database helper class you are using Activity context in this class) so better to make instance of DatabaseHandler
class and use method updateScores()
with instance.
将方法updateScores
更改为static
,如下所示:
public static void updateScores(List<Score> score)
DatabaseHandler dbh = new DatabaseHandler();
dbh.updateScores(listScore);
You can either make the Method
public void updateScores(List score)
as static or you can make an object of **DatabaseHandler **
in the caller class:
ScoreList
Error message tell you exectly problem: you reference non-static method in static manner. There are two possibilities: 1. You want have static method (see other answers) 2. You want have non-static method. Then use this caller snippet:
DatabaseHandler helper = new DatabaseHandler();
helper.updateScores(listScore);
Error is because of you are calling the non static method using class name .to overcome this error you have to do the any one of the following things 1 . make your method updateScores as static or 2 . create an instance of DatabaseHandler and call using that object call the method.
calling method using class name requires method to be a static
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.