我正在尝试动态地实现Radio Group
,但是我收到一条错误,说“孩子已经在父级上有一个父调用removeView()”。以下是我的代码:
RadioGroup rg = new RadioGroup(getContext());
RadioButton[] allSongsRadio = new RadioButton[countOfAllSongs];
ArrayList<String> allSongsInMap = new ArrayList<>(countOfAllSongs);
for(final String eachMood : allSongs.keySet()) {
for(String eachSong : allSongs.get(eachMood)) {
allSongsInMap.add(eachMood+" : "+eachSong);
}
}
for(int i=0;i<allSongsInMap.size();i++){
final String eachSong = allSongsInMap.get(i);
LinearLayout playButtonAndSeekBar = new LinearLayout(getContext());
final FloatingActionButton playButton = new FloatingActionButton(getContext());
playButton.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE));
playButton.setImageResource(R.drawable.play);
playButton.setSize(FloatingActionButton.SIZE_MINI);
playButton.setId(++playButtonId);
SeekBar seekBarForEachSong = new SeekBar(getContext());
layoutDetails = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutDetails.rightMargin = 50;
seekBarForEachSong.setLayoutParams(layoutDetails);
allSongsRadio[i] = new RadioButton(getContext());
playButtonAndSeekBar.addView(allSongsRadio[i]);
playButtonAndSeekBar.addView(playButton);
playButtonAndSeekBar.addView(seekBarForEachSong);
eachSongPanel.addView(playButtonAndSeekBar);
rg.addView(allSongsRadio[i]);
dialogContainer.addView(eachSongPanel);
}
dialogContainer.addView(rg);
}
需要帮助的人。
答案 0 :(得分:1)
您看到此错误导致您将相同的组件添加到ViewGroup
中不允许的两个Android
中(每个元素属于一个ViewGroup Parent),您正在这样做:
首先,您要在此处添加单选按钮:
playButtonAndSeekBar.addView(allSongsRadio[i]);
然后你将它添加到广播组:
rg.addView(allSongsRadio[i]);
此时allSongsRadio[i]
属于playButtonAndSeekBar
,您无法将其添加到另一个ViewGroup。
要纠正您的代码,只需将按钮添加到广播组,然后最后将整个广播组添加到playButtonAndSeekBar
,以了解您所需的设计。