Ich habe ein Fragment, in dem ich ein Element außer der Ansicht der Segmente aufblasen, die ein lineares Layout enthält. Es ist in Ordnung, aber jetzt ist das Problem, wenn ich es verweise, und versuche, ein TextView programmatisch in linearem Layout hinzuzufügen, verursacht es einen Fehler. Jede Hilfe wird bevorzugt.java.lang.IllegalStateException: wile das Hinzufügen von Ansichten zum Aufblasen von Layout programmgesteuert
Fehler: java.lang.IllegalStateException: Das angegebene Kind hat bereits ein Elternteil. Sie müssen zuerst removeView() für das übergeordnete Element des Kindes aufrufen.
Fragment.java
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_fragment
<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>
'row1.add View (getTextView (getTextView ("button_one", "button"))); '- Mit dem angegebenen Code, der nicht kompiliert wird und daher niemals diese Exception werfen könnte. Was ist der eigentliche Code? –
diese Zeile ersetzen row1.addView (getTextView (getTextView ("button_one", "button"))); mit row1.addView (getTextView ("button_one", "button")); – Meenal
Mike du hast recht, und es hat mein Problem gelöst. Ich habe meine Frage bearbeitet. Aber, können Sie mir den Grund dafür sagen? –