Android incremented counters wont reset?

2019-07-18 13:25发布

问题:

I'm trying to create a score tracker for GAA matches (Irish football).

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.

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

}

回答1:

Put initialization into onCreate method

 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;

    }
}


回答2:

Your counters are declared as static, so the values are shared by each instance of MainActivity. Basically each time you create MainActivity it will inherit the values from previous instance.