我有这个课程,我将这个名字命名为Scoreboard:
public class Scoreboard {
private int mScore = 0;
private int mHighScore = 0;
public void addPoint() {
mScore += 1;
}
public int getHighScore() {
return mHighScore;
}
public void setHighScore(int highScore) {
mHighScore = highScore;
}
public int getScore() {
return mScore;
}
public void setScore(int score) {
mScore = score;
}
}
我有一段代码在我的活动中,每当用户将球传递到矩形的间隙时操纵这个类,并相应地更改TextView以向用户显示分数:
//Keeps track of score
if (rectWhite1.getTheRight() > Math.round(mBallPos.x) - 5 && rectWhite1.getTheRight() < Math.round(mBallPos.x) + 5) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore());
}
});
Log.d(TAG, "If statement has been run3333!!!");
}
if (rectWhite2.getTheRight() > Math.round(mBallPos.x) - 5 && rectWhite2.getTheRight() < Math.round(mBallPos.x) + 5) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore());
}
});
Log.d(TAG, "If statement has been run2222!!!");
}
if (rectWhite3.getTheRight() > Math.round(mBallPos.x) - 5 && rectWhite3.getTheRight() < Math.round(mBallPos.x) + 5) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore());
}
});
Log.d(TAG, "If statement has been run1111!!!");
}
每当我运行应用程序时,只要我尝试使用与我制作的记分板类相关的任何方法,它就会崩溃。当我尝试添加一个点时它会崩溃,当我试图获得当前分数时它会崩溃,等等。所以我的问题是如何避免这种情况发生并动态改变分数?