2016-12-30 6 views
0

Radio Groupを動的に実装しようとしていますが、「子がすでに親にremoveView()を親に持っています」というエラーが表示されています。以下は私のコードです:RadioGroupでエラーが発生しました

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

完全なstacktraceを送信し、エラーの原因となったコードの行を示してください。 –

答えて

1

あなたは(各要素が1人のViewGroup親に属している)Androidでは許可されていない二つの異なるViewGroupに同じコンポーネントを追加している原因このエラーを見ているあなたがそうしているところ、そこにある:

まずここでのラジオボタンを追加している:

playButtonAndSeekBar.addView(allSongsRadio[i]); 

をそして、あなたはラジオ・グループに追加されます。

rg.addView(allSongsRadio[i]); 

この時点でallSongsRadio[i]playButtonAndSeekBarに属し、別のViewGroupに追加することはできません。

コードを修正するには、ラジオグループにボタンを追加してから、最後にラジオグループ全体をplayButtonAndSeekBarに追加してください。

+0

まだ同じエラーが発生しています。このように変更されました:seekBarForEachSong.setLayoutParams(layoutDetails); allSongsRadio [i] =新しいRadioButton(getContext()); rg.addView(allSongsRadio [i]); playButtonAndSeekBar.addView(playButton); playButtonAndSeekBar.addView(seekBarForEachSong); playButtonAndSeekBar.addView(rg); eachSongPanel.addView(playButtonAndSeekBar); dialogContainer.addView(eachSongPanel); } // dialogContainer.addView(rg); – Santanu

+0

別のエラーが発生しました。ここを見てください: 'playButtonAndSeekBar.addView(rg);'ループ内で、繰り返しごとに新しいLinearLayoutに 'rg'を追加しています。 –

+0

ありがとうたくさんの男。 :)私は何をしているのか分かっていたと思う。これらは私が行った変更ですが、今ではボタンを見ることはできますが、複数のボタンを選択することはできます。 – Santanu

関連する問題