8

I just updated to appcompat library from 23.1.1 to 23.2 and now application stopped working on java.lang.IllegalStateException: View can not be anchored to the the parent CoordinatorLayout.

java.lang.IllegalStateException: View can not be anchored to the the parent CoordinatorLayout

Crash comes from resolveAnchorView method of CoordinatorLayout when parent (CoordinatorLayout is not in edit mode). Layout is used as root element in base activity and contains different layouts (toolbar, progressbar and finally relativelayout with inflated layout of activity.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/base_activity__main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/shared__color_primary">

    <ProgressBar
        android:id="@+id/base_activity__progress_bar_top"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:visibility="gone" />

    <ViewSwitcher
        android:id="@+id/base_activity__switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/base_activity__progress_bar_top">

        <LinearLayout
            android:id="@+id/base_activity__progress_bar_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/_loading"
                android:textStyle="italic" />

            <ProgressBar
                android:id="@+id/base_activity__progress_bar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:indeterminate="true" />
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/base_activity__main_content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </ViewSwitcher>

    <RelativeLayout
        android:id="@+id/base_activity__add_button__container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" />

</android.support.design.widget.CoordinatorLayout>
RamenChef
  • 5,533
  • 11
  • 28
  • 39
jakub
  • 176
  • 1
  • 10
  • where is this layout `base_activity__main_layout` – Bhargav Mar 02 '16 at 12:58
  • it is coordinator laytout contains this xml, but problem was caused with this line app:layout_anchor="@id/base_activity__main_layout", but I still do not understand why – jakub Mar 02 '16 at 13:12
  • so the co ordinator layout is in a different file? and you are adding this layout that you have attached in your question by inflating it in a fragment? – Bhargav Mar 02 '16 at 14:13
  • no it is single xml file, example in question inside coordinatorlayout – jakub Mar 02 '16 at 15:02
  • is this `base_activity__add_button__container` a direct child of the cordinator layout? – Bhargav Mar 02 '16 at 15:18
  • full layout is updated in question now – jakub Mar 02 '16 at 15:35
  • 3
    seems that to "layout_anchor" must be assigned a control that is child of the CoordinatorLayout and not the coordinator layout itself. Try to set layout_anchor to ViewSwitcher for example – Luci Mar 02 '16 at 20:24

1 Answers1

17

You cannot set the layout anchor attribute to the co ordinator layout itself app:layout_anchor="@id/base_activity__main_layout", this is wrong, you need to set some direct child of the co ordinator as this layouts anchor

Bhargav
  • 7,584
  • 5
  • 37
  • 60