【发布时间】:2013-10-31 14:47:43
【问题描述】:
我必须在其他布局的子布局(线性布局)中设置布局。为此,我在要设置到根布局中的布局活动上编写以下代码:
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);
但我在最后一行 inside_menu_view.addView(this_layout); 得到一个 NULLPOINTEREXCEPTION
更新 - 在 super.onCreate 之后添加 setContentView()
【问题讨论】:
-
在访问
activitycontent布局之前,您在哪里调用setContentView? -
@ρяσѕρєя K 我在 super.onCreate 之后调用它...我更新了帖子
-
@codeMagic 否,
activitycontent是其他布局的子布局,我想在其中插入main布局 -
发布。堆。痕迹。和布局主要。
-
@njzk2 抱歉,我不明白你的意思
标签: android android-layout inflate