I was tweaking the sample hello world app that android studio provides and found out that I cannot call the setContentView(R.layout.activity_main);
outside any method.For example:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
setContentView(R.layout.activity_main); //compilation error
}
I know that I should not be calling setContentView
outside onCreate()
,but just for a reference I tried it out.I can figure out that this has something to do with Java and not android,but I can't seem to figure out the where the problem exactly lies.Any help will be appreciated.