title: String variables becomes null outside setOnClickListener() function
body: <p>I have a simple android app in which I want to get the user input (String). So I have an "EditText" box and an "Enter" button. When the user types something and hits Enter, I am trying to display that text and it works as long as I do all that inside the 'setOnClickListener()' function. The string variables becomes null outside the function. Here is my code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
   Button hitButton;
   EditText userInput;
   String myVariable;
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      hitButton = (Button)findViewById(R.id.button);
      hitButton.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View view) {
            userInput = (EditText)findViewById(R.id.userInput);
            myVariable= userInput.getText().toString();
  
         }
      });
  
       TextView textView = (TextView)findViewById(R.id.textView);
       textView.setText(myVariable); // THIS IS THE PROBLEM
       // IT DISPLAYS NULL WHICH MEANS THAT String myVariable = null
       }
    }
  </code></pre>
  
  <p>If I put TextView.setText(myVariable) inside the setOnClickListener function then it works perfectly but outside the function, myVariable becomes null. And I need to user that variable outside the function.</p>
  
  <p>Thanks in advance :)</p>