0

im trying to understand variable scope with simple example. I need help with this code

package com.varialescope.examplevariablescope;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button buttonOne;
private Button buttonTwo;
private String mText = "Hello World";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Initialialize UI elements

    buttonOne = (Button) findViewById(R.id.button_one);
    buttonOne = (Button) findViewById(R.id.button_two);

    //Button One click listener

    buttonOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Set new text

            mText = "ONE";

            Toast.makeText(MainActivity.this, mText,      Toast.LENGTH_SHORT).show();
        }
    });

    //Button Two click listener

    buttonTwo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(MainActivity.this, mText, Toast.LENGTH_SHORT).show();
        }
    });
}
}

How can i access to mText string from click listener method ?

and how can i set a new string for mText clicking button One and make it accessible globally?

thanks for help

5
  • 1
    What is the problem? Haven't you accessed mText from within OnClickListener? Commented Oct 8, 2016 at 17:06
  • You're already accessing mtext, but if you want to be explicit, qualify like this: MainActivity.this.mText Commented Oct 8, 2016 at 17:09
  • Can you run the app? I strongly believe that it should crash as soon as you start it Commented Oct 8, 2016 at 17:12
  • Yes @iulian the app crash. I cant understand how to set new global variable text clicking on button one then retrieve text clicking button two Commented Oct 8, 2016 at 20:26
  • I think that you code does what you want if you fix the crash. To do that, change buttonOne in buttonTwo where you do the second findViewById. The problem is that you assign 2 values to the same object, and the other one is null, thing that leads to crash Commented Oct 8, 2016 at 20:30

1 Answer 1

1

you create anonymous class Object for clicklistener any anonymous class or inner class object has information about the outside class object , then it had the right to access the methods and variables of the outside class object

Sign up to request clarification or add additional context in comments.

2 Comments

In this way the app crash. I cant understand how to set new global variable text clicking on button one then retrieve text clicking button two
it doesn't have reason to crash .. can you give us the error msg ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.