Variable access within inner class

0

I'm new to programming android / java and then this error came up.

public class Activity_2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity2);
    getSupportActionBar().hide();


    Spinner alvos = (Spinner) findViewById(R.id.alvos);
    Spinner posicoes = (Spinner) findViewById(R.id.posicoes);
    ImageButton hit = (ImageButton) findViewById(R.id.hit);
    ImageButton miss = (ImageButton) findViewById(R.id.miss);

    int contador = getIntent().getIntExtra("cont", 0);
    int sum = getIntent().getIntExtra("targ", 0);
    Integer [] arrayAlvos = new Integer[sum];

    for(int i=1; i<=sum; i++){
        arrayAlvos[i-1]=i;
    }
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_dropdown_item_1line, arrayAlvos);
    alvos.setAdapter(adapter);


    ArrayAdapter pos = ArrayAdapter.createFromResource(this, R.array.posicao, android.R.layout.simple_dropdown_item_1line);
    posicoes.setAdapter(pos);

        hit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(tiro.this);
                alert.setMessage("Certo");
                alert.show();
                contador++;
            }
        });

        miss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(tiro.this);
                alert.setMessage("Errado");
                alert.show();
                contador++;
            }
        });
   }
}

I created the variable int contador = getIntent().getIntExtra("cont", 0); to know the limit.

Then on the part of the buttons whenever I clicked a button it would increment +1 the counter variable. But in the part where I have contador++ it gives me this error:

  

variable counter is accessed from within inner class needs to be declared final android

Only I can not change to the end because my goal is to always be to change the value. Any solution?!

    
asked by anonymous 04.11.2016 / 11:58

1 answer

3

Local variables can not be accessed by nested classes, unless they are final or declared as the property of the main class that holds the nested class, in which case it would have to be of class Activity_2 .

public class Activity_2 extends AppCompatActivity {

  private int contador;
...

And within the onCreate method you just initialize it:

contador = getIntent().getIntExtra("cont", 0);

This would be one of the solutions, another would be to delegate the execution of the anonymous class to a method in its class Activity_2 , passing View as an argument, or even create a class that implements OnClickListener , containing the code execution, but by its code, seem to me solutions that are not necessary.

    
04.11.2016 / 12:07