gpt4 book ai didi

java - 尝试在模板 xml 文件的循环中以编程方式设置 View

转载 作者:行者123 更新时间:2023-12-01 13:11:20 25 4
gpt4 key购买 nike

我有这个 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="20dp" />

<Button
style="@style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:enabled="false" />

</LinearLayout>

我正在尝试使用此模板并将其设置为循环以动态再现它。我能够做到这一点。但我需要为每个循环的 subview (ButtonTextView)设置一些属性。

这是迄今为止我的代码:

int button_id = 0;
for (final CriticalElement e : skill.elements) {


// add LinearLayout row
LinearLayout LL = (LinearLayout) getActivity().getLayoutInflater()
.inflate(R.layout.motorskills_button_template, null);
llElementContainer.addView(LL);

final TextView tv = (TextView) ((ViewGroup) LL).getChildAt(0);
tv.setText(e.element);


Button b = (Button) ((ViewGroup) LL).getChildAt(1);
String tag = String.valueOf(e.element_num);
b.setTag(tag);
b.setId(button_id);
b.setEnabled(false);
LL.addView(b); // issue at this line

LL.addView(tv);
button_id++;
}

但是,我得到了这个堆栈跟踪:

04-03 08:42:10.326: E/AndroidRuntime(19019): FATAL EXCEPTION: main
04-03 08:42:10.326: E/AndroidRuntime(19019): Process: org.---.---, PID: 19019
04-03 08:42:10.326: E/AndroidRuntime(19019): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addView(ViewGroup.java:3360)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.view.ViewGroup.addView(ViewGroup.java:3336)
04-03 08:42:10.326: E/AndroidRuntime(19019): at org.---.---.SessionRecordMotorskillsFragment.onActivityCreated(SessionRecordMotorskillsFragment.java:125)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.os.Handler.handleCallback(Handler.java:733)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.os.Handler.dispatchMessage(Handler.java:95)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.os.Looper.loop(Looper.java:136)
04-03 08:42:10.326: E/AndroidRuntime(19019): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 08:42:10.326: E/AndroidRuntime(19019): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 08:42:10.326: E/AndroidRuntime(19019): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 08:42:10.326: E/AndroidRuntime(19019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 08:42:10.326: E/AndroidRuntime(19019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 08:42:10.326: E/AndroidRuntime(19019): at dalvik.system.NativeStart.main(Native Method)

最佳答案

如果删除此行:

 LL.addView(b);
LL.addView(tv);

你的程序将会工作,因为它们已经是LinearLayout LL的一部分

关于java - 尝试在模板 xml 文件的循环中以编程方式设置 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22842833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com