2015-01-08 67 views
3

即時通訊嘗試從字符串數組創建RadioButton,但它不工作,如果我只使用字符串,那麼它似乎工作正常 日誌貓顯示錯誤「ava.lang.IllegalStateException:已指定的子有一個家長,你必須先調用孩子父母的removeView()。「動態創建與字符串數組RadioButton

我知道錯誤是未來怎麼一回事,因爲單選按鈕對象不創建動態,從而使錯誤顯示

我把代碼從這裏參考,請查閱這個網址 http://android.okhelp.cz/create-radiobutton-radiogroup-dynamically-android-sample/

請檢查代碼並糾正我,所以我將與我的應用程序 走得更遠這裏是代碼

LinearLayout layout = (LinearLayout) findViewById(R.id.layout); 
 
RadioGroup radioGroup = new RadioGroup(this); 
 

 
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
 
       LinearLayout.LayoutParams.FILL_PARENT, 
 
       LinearLayout.LayoutParams.WRAP_CONTENT 
 
     ); 
 
\t \t 
 
\t \t // adding Radio Group 
 
     layout.addView(radioGroup, p); 
 
     
 
     // creating Radio buttons Object// 
 
     RadioButton radioButtonView = new RadioButton(this); 
 
     String[] ab ={"1","2"}; 
 
     
 
     for(int i =0; i<ab.length;i++) 
 
     { 
 
      radioButtonView.setText(ab[i]); 
 
      radioGroup.addView(radioButtonView, p); 
 
      ((ViewGroup)layout.getParent()).removeView(layout); 
 
     } 
 
    

和日誌貓錯誤

I/Choreographer(4431): Skipped 547 frames! The application may be doing too much work on its main thread. 
 
D/AndroidRuntime(4431): Shutting down VM 
 
W/dalvikvm(4431): threadid=1: thread exiting with uncaught exception (group=0xb2d2fb20) 
 
    FATAL EXCEPTION: main 
 
    Process: com.example.radiobuttondynamic, PID: 4431 
 
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
 
    \t at android.view.ViewGroup.addViewInner(ViewGroup.java:3562) 
 
    \t at android.view.ViewGroup.addView(ViewGroup.java:3415) 
 
    \t at android.widget.RadioGroup.addView(RadioGroup.java:141) 
 
    \t at android.view.ViewGroup.addView(ViewGroup.java:3391) 
 
    \t at com.example.radiobuttondynamic.Radio_Button.onCreateOptionsMenu(Radio_Button.java:46) 
 
    \t at android.app.Activity.onCreatePanelMenu(Activity.java:2538) 
 
    \t at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436) 
 
    \t at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800) 
 
    \t at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221) 
 
    \t at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
 
    \t at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
 
    \t at android.view.Choreographer.doFrame(Choreographer.java:543) 
 
    \t at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
 
    \t at android.os.Handler.handleCallback(Handler.java:733) 
 
    \t at android.os.Handler.dispatchMessage(Handler.java:95) 
 
    \t at android.os.Looper.loop(Looper.java:136) 
 
    \t at android.app.ActivityThread.main(ActivityThread.java:5017) 
 
    \t at java.lang.reflect.Method.invokeNative(Native Method) 
 
    \t at java.lang.reflect.Method.invoke(Method.java:515) 
 
    \t at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
 
    \t at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
 
    \t at dalvik.system.NativeStart.main(Native Method) 
 
D/dalvikvm(4431): GC_FOR_ALLOC freed 142K, 7% free 3094K/3308K, paused 107ms, total 113ms

尋找幫助 感謝

+0

您的XML如何看起來像? – Lobato

回答

3

IllegalStateException異常:指定的孩子已經有一個父。您必須先調用子視圖的父視圖上的removeView(),您必須調用 。

因爲您在LinearLayout中再次添加相同的RadioButton實例。

在for循環中創建的RadioButton對象:

for(int i =0; i<ab.length;i++) 
    { 
     RadioButton radioButtonView = new RadioButton(this); 
     radioButtonView.setText(ab[i]); 
     radioGroup.addView(radioButtonView, p); 
     ((ViewGroup)layout.getParent()).removeView(layout); 
    } 

在for循環中,以避免錯誤。

+0

你做到了,我過去三天都在找這個。 –

+0

我最後一個問題,我需要**從頂部**給予Marign,我怎麼能給它? –

+0

@AshuKumar:您可以通過設置「p」來爲所有RadioButton設置保證金。setMargins('left,top,right,bottom);' –

1

的錯誤是,你正試圖在相同的佈局添加相同的單選按鈕幾次,你必須移動的單選框創建內部的循環

String[] ab ={"1","2"}; 

    for(int i =0; i<ab.length;i++) 
    { 
     RadioButton radioButtonView = new RadioButton(this); 
     radioButtonView.setText(ab[i]); 
     radioGroup.addView(radioButtonView, p); 
     ((ViewGroup)layout.getParent()).removeView(layout); 
    } 
2

如果你有這樣的數組: -

String route[] = {"1", "3", "4"}; 

您可以使用循環創建單選按鈕: -

for (int i = 0; i < route.length; i++) 
{ 
    RadioButton radioButton = new RadioButton(this); 
    radioButton.setText("Route " + String.valueOf(route[i])); 
    radioButton.setId(i); 
    rprms = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); 
    rgp.addView(radioButton, rprms); 
} 

你可以使用以下方式獲取所選字段: - (我有SAVE按鈕,因此此代碼是用onClick監聽器編寫的) 否則您也可以使用onCheckedChangeListener。

int selection = rgp.getCheckedRadioButtonId(); 
for (int i = 0; i < route.length; i++) 
{ 
    if (i == selection) 
     showToast("" + route[i]); 
}