[android] java.lang.IllegalStateException: The specified child already has a parent

I had this problem and couldn't solve it in Java code. The problem was with my xml.

I was trying to add a textView to a container, but had wrapped the textView inside a LinearLayout.

This was the original xml file:

_x000D_
_x000D_
<?xml version="1.0" encoding="utf-8"?>_x000D_
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"_x000D_
    android:orientation="vertical"_x000D_
    android:layout_width="match_parent"_x000D_
    android:layout_height="match_parent">_x000D_
    _x000D_
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"_x000D_
        android:id="@android:id/text1"_x000D_
        android:layout_width="match_parent"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:textAppearance="?android:attr/textAppearanceListItemSmall"_x000D_
        android:gravity="center_vertical"_x000D_
        android:paddingLeft="16dp"_x000D_
        android:paddingRight="16dp"_x000D_
        android:textColor="#fff"_x000D_
        android:background="?android:attr/activatedBackgroundIndicator"_x000D_
        android:minHeight="?android:attr/listPreferredItemHeightSmall"/>_x000D_
_x000D_
</LinearLayout>
_x000D_
_x000D_
_x000D_

Now with the LinearLayout removed:

_x000D_
_x000D_
<TextView xmlns:android="http://schemas.android.com/apk/res/android"_x000D_
        android:id="@android:id/text1"_x000D_
        android:layout_width="match_parent"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:textAppearance="?android:attr/textAppearanceListItemSmall"_x000D_
        android:gravity="center_vertical"_x000D_
        android:paddingLeft="16dp"_x000D_
        android:paddingRight="16dp"_x000D_
        android:textColor="#fff"_x000D_
        android:background="?android:attr/activatedBackgroundIndicator"_x000D_
        android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
_x000D_
_x000D_
_x000D_

This didn't seem like much to me but it did the trick, and I didn't change my Java code at all. It was all in the xml.

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to android-fragments

FragmentActivity to Fragment How to start Fragment from an Activity How to use data-binding with Fragment In android how to set navigation drawer header image and name programmatically in class file? Android Fragment onAttach() deprecated How to convert any Object to String? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments java.lang.IllegalStateException: Fragment not attached to Activity java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference