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));
1 つはカスタム ビューで、もう 1 つは XML からのものです。そのうちの1つにコメントしましたが、どちらも投げているようですIllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
ビューを最初に「コンテナ」に入れる(別のレイアウト)、またはremoveView((View)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) に属している必要がありました。これはうまくいかなかったようです。
ありがとう