简体   繁体   English

Java,Android和SharedPreferences和OOP

[英]Java & Android & SharedPreferences & OOP

The function contains in the main Activity: 该函数包含在主活动中:

public int checkScore(int scoreCurrent) {
         int maxscore = PreferenceConnector.readInteger(this, "maxscore", 0);
         if (scoreCurrent > maxscore) {
          PreferenceConnector.writeInteger(this, "maxscore",
                         scoreCurrent);
          maxscore = scoreCurrent;
         }
         return maxscore;
        }

The class PreferenceConnector simplifies work with SharedPreferences. PreferenceConnector类简化了SharedPreferences的工作。 Function checkScore() should be available in other classes, so need to do static. 函数checkScore()应该在其他类中可用,因此需要进行静态处理。 But i have error: 但是我有错误:

Cannot use this in a static context

What to do and how to fix? 怎么办以及如何解决?

Activity: 活动:

public class GameScreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public static int checkScore(int scoreCurrent) {
        int maxscore = PreferenceConnector.readInteger(GameScreen.this,
                "maxscore", 0);
        if (scoreCurrent > maxscore) {
            PreferenceConnector.writeInteger(GameScreen.this,
                    "maxscore", scoreCurrent);
            maxscore = scoreCurrent;
        }
        return maxscore;
    }
}

You have to pass CONTEXT of your ACTIVITY to the class PreferenceConnector 您必须将ACTIVITY的上下文传递给PreferenceConnector类

And you have to make the object of the class PreferenceConnector, at that time you can pass your Activity's Context to that class. 并且您必须创建类PreferenceConnector的对象,这时您可以将Activity的Context传递给该类。

And don't make the method writeInteger(this, "maxscore", scoreCurrent); 并且不要使方法writeInteger(this,“ maxscore”,scoreCurrent); STATIC 静态的

Use it by creating the object of PreferenceConnector class in your Main Activity. 通过在您的主活动中创建PreferenceConnector类的对象来使用它。

Try this below code: 试试下面的代码:

public int checkScore(int scoreCurrent) {
         int maxscore = PreferenceConnector.readInteger(YourActivityName.this, "maxscore", 0);
         if (scoreCurrent > maxscore) {
          PreferenceConnector.writeInteger(YourActivityName.this, "maxscore",
                         scoreCurrent);
          maxscore = scoreCurrent;
         }
         return maxscore;
        }

(or) (要么)

public int checkScore(int scoreCurrent) {
             int maxscore = PreferenceConnector.readInteger(getApplicationContext(), "maxscore", 0);
             if (scoreCurrent > maxscore) {
              PreferenceConnector.writeInteger(getApplicationContext(), "maxscore",
                             scoreCurrent);
              maxscore = scoreCurrent;
             }
             return maxscore;
            }

You could just pass a reference to the GameScreen activity to your other classes. 您可以将对GameScreen活动的引用传递给其他类。 Then you don't need anything to be static. 然后,您不需要任何东西都是静态的。 You could just call myGameScreen.checkScore() . 您可以只调用myGameScreen.checkScore()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM