【问题标题】:Java Error: Cannot make a static reference to the non-static methodJava 错误:无法对非静态方法进行静态引用
【发布时间】:2013-02-07 06:10:40
【问题描述】:

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

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 类。我尝试将函数设为静态,但由于错误,它无法以这种方式工作。

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

【问题讨论】:

标签: java static


【解决方案1】:

因为您正在使用静态引用访问方法

DatabaseHandler.updateScores(listScore);   

表示类名..

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

或将updateScores 设为静态方法,例如,

static public void updateScores()

但我怀疑您在DatabaseHandler 类中有更多代码。 (作为它的 sqlite 数据库助手类,您在此类中使用 Activity 上下文)所以更好地制作 DatabaseHandler 类的 instance 并将方法 updateScores() 与实例一起使用。

【讨论】:

  • 你能给我一个第一个例子吗?我已经尝试过将方法设为静态,正如您所猜测的那样,当我这样做时,我还有其他代码停止工作。
  • 好的,在你的应用程序活动中 create()DatabaseHandler dbHelper = new DatabaseHandler(this); 而在 updateListView() 只需写 dbHelper.updateScores(listScore); 确保 DatabaseHandler dbHelperScoreList 中的全局变量。跨度>
【解决方案2】:
public void updateScores(List<Score> score)

改成

public static void updateScores(List<Score> score) 

为什么?

因为你正在使用 static 调用

DatabaseHandler.updateScores(listScore)

【讨论】:

    【解决方案3】:

    将方法updateScores改为static如下:

     public static void updateScores(List<Score> score)
    

    【讨论】:

      【解决方案4】:
      DatabaseHandler dbh = new DatabaseHandler();
      dbh.updateScores(listScore);
      

      【讨论】:

      • 这样做并得到“构造函数 DatabaseHandler() 未定义”
      • 您在我发帖后更新了代码。因此,您在 DatabaseHandler 中有参数化的构造函数。所以最好将 updateScores 作为静态方法
      【解决方案5】:

      你可以制作方法

      public void updateScores(列表分数)

      作为 static 或者您可以在调用者类中创建 **DatabaseHandler ** 的对象:

      分数列表

      【讨论】:

        【解决方案6】:

        错误信息告诉你问题:你以静态方式引用非静态方法。 有两种可能: 1.你想要静态方法(见其他答案) 2.你想要非静态方法。然后使用这个调用者sn-p:

        DatabaseHandler helper = new DatabaseHandler();
        helper.updateScores(listScore);
        

        【讨论】:

          【解决方案7】:

          错误是因为您使用类名调用非静态方法。要克服此错误,您必须执行以下任何一项操作 1.使您的方法 updateScores 为静态或 2.创建一个 DatabaseHandler 实例并使用该对象调用该方法。

          使用类名调用方法要求方法是静态的

          【讨论】:

            猜你喜欢
            • 2013-03-31
            • 2013-06-30
            • 2019-07-13
            • 2011-06-25
            • 2014-11-19
            相关资源
            最近更新 更多