问题描述:

I know that the function onCreate() used to construct the activity and UI components but when I try put setContentView(R.layout.activity_main) in the function onStart() or onResume(), the UI components apper , that's why?

package com.example.E001;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

}

protected void onStart() {

super.onStart();

setContentView(R.layout.activity_main);

//setContentView(R.layout.demo);

Button btn_welcome = (Button)this.findViewById(R.id.welcome_msg);

btn_welcome.setText(R.string.hello_world);

}

}

网友答案:

You should put it in the onCreate, like this:

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

For more info look in the Android Documentation

What I think you are trying to do with the layout.demo is inflate a extra view in your MainActivity. Then you should NOT use setContentView(). For inflating a view in a layout you can use the LayoutInflater.

Note: If this does not completely answer your question, than I don't understand your question correctly.

相关阅读:
Top