即時通訊嘗試從字符串數組創建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
尋找幫助 感謝
您的XML如何看起來像? – Lobato