Android java.lang.IllegalStateException:以编程方式向膨胀布局添加视图
我有一个片段,在其中我膨胀了一个项目,而不是frmings视图,它包含一个线性布局。它膨胀得很好,但现在的问题是,当我引用它,并试图以编程方式在线性布局中添加TextView时,它会出错。任何帮助都会更好 错误:java.lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用removeView() Fragment.javaAndroid java.lang.IllegalStateException:以编程方式向膨胀布局添加视图,android,android-studio,android-fragments,layout-inflater,android-inflate,Android,Android Studio,Android Fragments,Layout Inflater,Android Inflate,我有一个片段,在其中我膨胀了一个项目,而不是frmings视图,它包含一个线性布局。它膨胀得很好,但现在的问题是,当我引用它,并试图以编程方式在线性布局中添加TextView时,它会出错。任何帮助都会更好 错误:java.lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用removeView() Fragment.java public class ShortFrag extends Fragment{ ListView list
public class ShortFrag extends Fragment{
ListView listview;
LinearLayout row1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.r_fragment, container, false);
listview=(ListView)view.findViewById(R.id.listview);
LayoutInflater myinflater = getLayoutInflater();
ViewGroup myHeader = (ViewGroup) myinflater.inflate(R.layout.helper, listview, false);
row1= headerViewHolder.findViewById(R.id.linear);
TextView tv = getTextView(getTextView("button_one","button"));
//error here
row1.addView(tv);
return view;
}
private TextView getTextView(String text, String tag) {
TextView textView = new TextView(getActivity());
textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
textView.setTag(tag);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewPager.LayoutParams.WRAP_CONTENT
);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
textView.setText(text);
textView.setPadding(10, 5, 10, 5);
params.setMargins(10, 0, 10, 0);
return textView;
}
}
r_片段
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<Listview
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
helper.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"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"/>
</LinearLayout>
试试这个:
//error here
row1.removeAllViews();
row1.addView(tv);
row1.addView(getTextView(getTextView(“按钮一”,“按钮”))代码>-对于给定的代码,它不会编译,因此永远不会引发该异常。实际代码是什么?替换此行row1.addView(getTextView(getTextView(“button_one”,“button”));使用row1.addView(getTextView(“按钮一”,“按钮”));迈克,你是对的,它解决了我的问题。我编辑了我的问题。但是,你能告诉我这样做的原因吗?嗯,不是真的,因为这也不会编译:TextView tv=getTextView(getTextView(“button_one”,“button”)代码>。您没有可以接受TextView
参数的getTextView()
方法。我不知道你做了什么来修复那个特殊的异常。