我有一个ViewSwitcher和希望的意见添加到它:View.addView()抛出IllegalStateException异常(ViewSwitcher)`
// initialize views
final ViewSwitcher switcher = new ViewSwitcher(this);
layMenu = (LinearLayout)findViewById(R.id.menu_main_view);
final LevelPicker levelPicker = new LevelPicker(getApplicationContext());
(//)switcher.addView(layMenu);
(//)switcher.addView(findViewById(R.layout.menu_switcher));
一个是一个自定义视图,从XML另外一个。我评论他们中的一个,但他们似乎都扔IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我想这样做就像把意见在“容器”第一个(另一个布局)几件事情,或者试图removeView((视图)的getParent),就像我相信在logcat的尝试说..
这里是我的xml文件(简单地说):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_main_view">
<TextView>
</TextView>
<LinearLayout>
<Button></Button> //couple of buttons
</LinearLayout>
</LinearLayout> //this is the parent i guess
我的第一个猜测是,所有儿童的必须是在1个母,这在我的情况是LinearLayout中。这似乎没有工作。
感谢
我很困惑。即我的LevelPicker是一个扩展LinearLayout的自定义类,在构造函数中我调用this.addView(inflater.inflate(R.layout ...)。我该如何正确地做到这一点?还应该是什么时候做Xml的内容你在上面描述的是什么? – user717572 2011-12-31 13:42:24
LayoutInflater适用于xml布局文件,在LevelPicker的情况下,只需简单地用java new运算符创建实例,或者如果你喜欢,你也可以使用xml文件来描述基于它的UI就像你在xml中使用正常的LinearLayout一样,唯一的区别是你应该使用LevelPicker的限定名作为xml标签,即 –
chyou
2012-01-02 12:24:40
您在Activity.this.setContentView()调用中使用的任何有效布局xml文件都可以由LayoutInflater“膨胀”。 – chyou 2012-01-02 12:26:01