Access variable inside Inner class

Asked

Viewed 126 times

0

I’m new in android/java programming and then I came up with this error.

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 in the buttons part whenever I clicked a button it incremented +1 to the counter variable. Only in the part where I have contador++ he gives me this mistake:

variable contador is accessed from Within Inner class needs to be declared final android

only that I can’t change to the end because my goal is to always change the value. Some solution?!

1 answer

3

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

public class Activity_2 extends AppCompatActivity {

  private int contador;
...

And within the method onCreate 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 separate method, within its class Activity_2, passing to View as argument, or even create a class that implements OnClickListener, containing the execution of the code, but by its code, seem to me solutions that are not necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.