我必须在其他布局的子布局(linearlayout)中设置布局。为此,我在要设置到根布局中的布局活动上编写以下代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**setContentView(R.layout.main);**
/**Define root layout's child where I want to set the layout*/
LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);
/**Inflate this layout and add it to the root layout*/
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View this_layout = inflater.inflate(R.layout.main, null);
inside_menu_view.addView(this_layout);
但我在最后一行得到一个 NULLPOINTEREXCEPTIONinside_menu_view.addView(this_layout);
更新 - 在 super.onCreate 之后添加了 setContentView()