Android递增计数器不会重置? [英] Android incremented counters wont reset?
本文介绍了Android递增计数器不会重置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为GAA比赛(爱尔兰足球)创建一个得分跟踪器.
I'm trying to create a score tracker for GAA matches (Irish football).
我已经开始运行它,但是每次运行该应用程序时,它不会将计数器重置为0,而是继续递增.
I've got it working but every time I run the app it doesn't reset the counters back to 0 and instead just continues to increment.
这是我的MainActivity java文件中的代码:
Here's the code in my MainActivity java file:
public class MainActivity extends ActionBarActivity {
public static int homeGoalsCounter = 0;
public static int homePointsCounter = 0;
public static int awayGoalsCounter = 0;
public static int awayPointsCounter = 0;
Button homeGoalButton;
Button homePointButton;
Button awayGoalButton;
Button awayPointButton;
TextView homeGoalsTextView;
TextView homePointsTextView;
TextView awayGoalsTextView;
TextView awayPointsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeGoalButton = (Button)(findViewById(R.id.homeGoal));
homePointButton = (Button)(findViewById(R.id.homePoint));
awayGoalButton = (Button)(findViewById(R.id.awayGoal));
awayPointButton = (Button)(findViewById(R.id.awayPoint));
homeGoalsTextView = (TextView)(findViewById(R.id.homeGoals));
homePointsTextView = (TextView)(findViewById(R.id.homePoints));
awayGoalsTextView = (TextView)(findViewById(R.id.awayGoals));
awayPointsTextView = (TextView)(findViewById(R.id.awayPoints));
homeGoalButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
homeGoalsCounter++;
homeGoalsTextView.setText(Integer.toString(homeGoalsCounter));
}
});
homePointButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
homePointsCounter++;
homePointsTextView.setText(Integer.toString(homePointsCounter));
}
});
awayGoalButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
awayGoalsCounter++;
awayGoalsTextView.setText(Integer.toString(awayGoalsCounter));
}
});
awayPointButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
awayPointsCounter++;
awayPointsTextView.setText(Integer.toString(awayPointsCounter));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
推荐答案
将初始化放入onCreate
方法
public class MainActivity extends ActionBarActivity {
public static int homeGoalsCounter ;
public static int homePointsCounter;
public static int awayGoalsCounter;
public static int awayPointsCounter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeGoalsCounter = 0;
homePointsCounter = 0;
awayGoalsCounter = 0;
awayPointsCounter = 0;
}
}
这篇关于Android递增计数器不会重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文