user2890391
user2890391

Reputation:

Java - Cannot refer to a non-final variable

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

Upvotes: 1

Views: 652

Answers (6)

orzangleli
orzangleli

Reputation: 178

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

Upvotes: 0

Ian Newson
Ian Newson

Reputation: 7939

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.

Upvotes: 0

Gopal Gopi
Gopal Gopi

Reputation: 11131

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...

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

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..

Upvotes: 2

Keerthivasan
Keerthivasan

Reputation: 12880

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.

Upvotes: 0

Jagadesh Seeram
Jagadesh Seeram

Reputation: 2664

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..

Upvotes: 1

Related Questions