Right.

The scope of aUIXButton is the inside of the loop, it does not persist until the later point in time when OnClickListener subclass is invoked.

Moreover, the value of aUIXButton refers to all five buttons, one by one, as the loop rolls.

Use this instead:

     aUIXButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
* Button button = (Button) arg0;
    button.setText("Y");
*    }});

-- Kostya

07.09.2010 16:00, Sara Khalatbari ?????:
Hi.

I have some dynamic buttons that are being generated inside a for loop.

    for (int i = 0; i < 5 ; i++) {


    Button aUIXButton = new Button (SecondPage.this);

    aUIXButton.setClickable(true);

    aUIXButton.setText("X");


    aUIXButton.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

    aUIXButton.setText("Y");

    }});

    }


This is not working. It says
"Cannot refer to a non-final variable aUIXButton inside an inner class defined in a different method"

& wants to change the modifier of aUIXButton to final & I don't want to do that.

Does anyone know how can I fix this?

Thank you,

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to