我正试图为GAA比赛(爱尔兰足球)创建一个得分跟踪器。
我已经开始工作,但每次运行应用程序时,它都不会将计数器重置为0,而是继续增加。
这是我的MainActivity java文件中的代码:
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);
}
}
答案 0 :(得分:2)
将初始化放入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;
}
}
答案 1 :(得分:1)
您的计数器被声明为static
,因此每个MainActivity实例共享这些值。基本上每次创建MainActivity时,它都将继承前一个实例的值。