I’m trying to switch the content of an activity using the setContentView in a similar manner:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (condition) {
setContentView(R.layout.layout1);
} else {
setContentView(R.layout.layout2);
}
...
}
In the folders:
layout1.xml
layout2.xml
It works but if after setConentView I want to reference a control in the layout2..like a TextView, using FindViewById ..I get an error and the program crashes.
What’s the problem. ?
If you want to reference a
Viewfrom one of your layouts in anActivity, you need to:setContentView()method.