Java 错误:无法对非静态方法进行静态引用 [英] Java Error: Cannot make a static reference to the non-static method

查看:17
本文介绍了Java 错误:无法对非静态方法进行静态引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Android 应用程序并收到此错误,但我不知道为什么.有人能帮我理解为什么我会收到这个错误吗?

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

这是相关的代码.

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);    
    }
}

这是 DatabaseHandler 类.我尝试将函数设为静态,但由于错误,它不会以这种方式工作.

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());    
        }       
    }
}

推荐答案

因为你访问的是静态引用的方法

Because you are accessing the method with static reference

DatabaseHandler.updateScores(listScore);   

表示带有类名..

您必须创建一个 DatabaseHandler 类的实例并使用该方法.

You have to make a instance of DatabaseHandler class and use that method.

或者将 updateScores 作为静态方法,

Or make a updateScores as static method like,

static public void updateScores()

但我怀疑您在 DatabaseHandler 类中有更多代码.(作为它的 sqlite 数据库助手类,你在这个类中使用 Activity 上下文)最好制作 DatabaseHandler 类的 instance 并使用方法 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.

这篇关于Java 错误:无法对非静态方法进行静态引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆