android 获取某个格局控件 添加到另一个布局中

android 获取某个布局控件 添加到另一个布局中

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

LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.layout456);
ImageView imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.ic_launcher);
relativeLayout.addView(imgApple2);

LayoutInflater factorys = LayoutInflater.from(MainActivity.this);
final View textEntryView = factorys.inflate(R.layout.layout1, null);

// LinearLayout linearLayout = (LinearLayout) textEntryView
// .findViewById(R.id.layout1);
// relativeLayout.addView(linearLayout);
EditText editText1 = (EditText) textEntryView
.findViewById(R.id.editText1);

relativeLayout.addView(editText1);

}





上面中 我要的获取另一个布局layout1里面的控件editText1 , 添加到主要布局中,为什么运行后提示为null。


如果我如下面写,又不会出错,而且成功添加
 LinearLayout linearLayout = (LinearLayout) textEntryView
 .findViewById(R.id.layout1);
 relativeLayout.addView(linearLayout);


上面是把另一个布局全加入,我要的是布局的某个控件。求解,谢谢!!

------解决方案--------------------
楼主,一句话,任何一个子控件同时只能存在于一个父控件布局中。
因此editText1 作为layout1的子控件,只能存在于layout1中,它只能是layout1的儿子,这种血缘关系不能再随便更改,而layout1由于是整个布局,它可以是layout456它的儿子。