-3

If I define the following lines

myText =(TextView)findViewById(R.id.textView); 
button=(Button)findViewById(R.id.button); 

in onCreate the program runs smoothly but if I do that in Main Activity then it causes an error.

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

public class MainActivity extends AppCompatActivity {

    myText =(TextView)findViewById(R.id.textView); //error 
    button=(Button)findViewById(R.id.button);

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(myText.getVisibility()==View.VISIBLE){

                    myText.setVisibility(View.INVISIBLE);
                }else
                {
                    myText.setVisibility(View.VISIBLE);
                }

            }
        });
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
Jay
  • 53
  • 1
  • 10

4 Answers4

1
  1. You should move

    myText =(TextView)findViewById(R.id.textView); 
    button=(Button)findViewById(R.id.button);
    

    to onCreate method / after setContentView, otherwise you will get nullPointerException. Initialize happened inside onCreate method.

  2. You have to define the Widget

     TextView myText =(TextView)findViewById(R.id.textView); 
     Button button=(Button)findViewById(R.id.button);
    
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

Write this code

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myText =(TextView)findViewById(R.id.textView); //error 
    button=(Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       if(myText.getVisibility()==View.VISIBLE){
             myText.setVisibility(View.INVISIBLE);
         }else
         {
             myText.setVisibility(View.VISIBLE);
         }
        }
    });
}
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
Pratik
  • 452
  • 4
  • 17
0

In onCreate() you call

setContentView(R.layout.activity_main);

Only at that point does the activity have a layout, and findViewById makes sense - it will now recursively look for a view within R.layout.activity_main that has the ID your looking for.

The code you write outside the onCreate method runs on class instantiation, which doesn't help you as it's before the layout as been set.

Try something like this:

public class MyActivity ... {
  private TextView foo;
  private Button bar;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    foo = (TextView)findViewById(R.id.some_id);
    bar = (Button)findViewById(R.id.some_other_id);

    // ... can use foo and bar here and during the rest of the activity lifecycle
  }

  // ...
}
orip
  • 73,323
  • 21
  • 116
  • 148
0

In the case of an Activity, findViewById starts the search from the content view (set with setContentView) of the activity which is the view hierarchy inflated from the layout resource.

So until in the onCreate the setContentView is called..findViewById wont work.

Java initializes class variables at class load time. If you make "myText" and "button" as class variables and initializes with findViewById , it just wont work because setContentView has not been called yet

Shubham Goel
  • 1,962
  • 17
  • 25