1

New to android. What should I edit to get rid of a strange "cannot refer to a non-final variable button inside an inner class defined in a different method?

@Override
    public void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        //Listeneři
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                button1.setText("ok");
JoxTraex
  • 13,423
  • 6
  • 32
  • 45

6 Answers6

2

What you are doing is not possible.

JLS # chapter 8

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class.

Make that variable as final before using it in innerclass,So that you can get permission to use it.

final Button button1 = (Button)findViewById(R.id.button2);

Like wise, make remaining also final, If you are using them inside listener..

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Change to this..

 final Button button1 = (Button)findViewById(R.id.button2);
    //Listeneři
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setText("ok");

or

you can declare the button variable reference as global..

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

You need to set the button variables as 'final' as below

final Button button1 = (Button)findViewById(R.id.button1);
final Button button2 = (Button)findViewById(R.id.button2);

so,that button1 and button2 cannot be re-initialized.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

try this...

 @Override
 public void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1 = (Button)findViewById(R.id.button1);
    Button button2 = (Button)findViewById(R.id.button2);
    //Listeneři
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Button button = (Button)v;
            button.setText("ok");
        }
    }

no need make the variable as final also...

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

Are you using Eclipse? If so, using the quick fix menu will help you solve issues such as non-final variables, and missing import statements.

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-quickfix-assist.htm

On Windows, place the cursor on the error and press Ctrl+1 to open the quick fix menu. Pressing enter will (usually) solve the error satisfactorily.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
0

U have two ways to slove the problem, 1. change Button button1 = (Button)findViewById(R.id.button1);

to final Button button1 = (Button)findViewById(R.id.button1);

2.define the button1 out of the function.like this:

public class MainActivity extends Activity
{
    Button button1 = (Button)findViewById(R.id.button1);
    Button button2 = (Button)findViewById(R.id.button2);
@Override
public void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.activity_main);

    //Listeneři
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            button1.setText("ok");
orzangleli
  • 178
  • 7
  • Surely that will try to find the buttons when the activity is created (ie before onCreate is called), but the buttons don't exist until setContentView is called...? – Jules Oct 26 '13 at 13:03