-2

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.

avistein
  • 102
  • 1
  • 8

1 Answers1

1

As per activity life cycle onCreate() is the method called when the activity is first created

OnCreate() is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri , String[], String, String[], String) to

Community
  • 1
  • 1
Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38