Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppBarLayout ScrollingViewBehavior is giving me a "java.lang.RuntimeException: This graph contains cyclic dependencies" error

So I keep getting a "java.lang.RuntimeException: This graph contains cyclic dependencies" error that I'm pretty sure is caused by a Behavior I have that extends from AppBarLayout.ScrollingViewBehavior. I have a layout with an AppBarLayout that uses this Behavior and the error disappears if I remove the behavior. Here's the error and the code for the Behavior

E/AndroidRuntime: FATAL EXCEPTION: main
Process: yu.heetae.android.mergingtoolbar, PID: 26027
java.lang.RuntimeException: This graph contains cyclic dependencies
at android.support.design.widget.DirectedAcyclicGraph.dfs(DirectedAcyclicGraph.java:164)
at android.support.design.widget.DirectedAcyclicGraph.dfs(DirectedAcyclicGraph.java:172)
at android.support.design.widget.DirectedAcyclicGraph.dfs(DirectedAcyclicGraph.java:172)
at android.support.design.widget.DirectedAcyclicGraph.getSortedList(DirectedAcyclicGraph.java:152)
at android.support.design.widget.CoordinatorLayout.prepareChildren(CoordinatorLayout.java:658)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:706)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
public class MergedAppbarBehavior extends AppBarLayout.ScrollingViewBehavior {

    private Toolbar mToolbar;
    private View mBackground;
    private FrameLayout.LayoutParams mBackgroundLayoutParams;

    private Context mContext;

    private boolean isInit = false;

    public MergedAppbarBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof NestedScrollView;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        if(!isInit) {
            init(child);
            return false;
        }

        if(isDependencyYBelowToolbar(child, dependency) && ! isDependencyYReachTop(dependency)) {
            setToolbarBackground(android.R.color.transparent);
            setPartialBackgroundHeight((int)((child.getHeight() + child.getY()) - dependency.getY()));
        } else if(isDependencyYBelowStatusToolbar(child, dependency) || isDependencyYReachTop(dependency)) {
            setToolbarBackground(Color.parseColor("#3F51B5"));
            setPartialBackgroundHeight(0);
        }

        return false;
    }

    private void init(View child) {
        AppBarLayout appBarLayout = (AppBarLayout) child;

        mToolbar = (Toolbar) appBarLayout.findViewById(R.id.toolbar);
        mToolbar.setTitle(" ");
        mBackground = appBarLayout.findViewById(R.id.background_view);
        mBackgroundLayoutParams = (FrameLayout.LayoutParams) mBackground.getLayoutParams();

        setToolbarBackground(android.R.color.transparent);
        setPartialBackgroundHeight(0);

        isInit = true;
    }

    private boolean isDependencyYBelowToolbar(View child, View dependency){
        return dependency.getY() <= child.getY() + child.getHeight() && dependency.getY() > child.getY();
    }

    private boolean isDependencyYBelowStatusToolbar(View child, View dependency){
        return dependency.getY() <= child.getY();
    }

    private boolean isDependencyYReachTop(View dependency){
        return dependency.getY() == 0;
    }

    private void setPartialBackgroundHeight(int height){
        mBackgroundLayoutParams.height = height;
        mBackground.setLayoutParams(mBackgroundLayoutParams);
    }

    private void setToolbarBackground(int color) {
        mToolbar.setBackgroundColor(ContextCompat.getColor(mContext, color));
    }
}
like image 790
user1840378 Avatar asked Oct 25 '16 21:10

user1840378


2 Answers

I had the same problem with a FloatingActionButton. I just removed this line from my XML

app:layout_anchor="@id/list_view"
like image 65
Buneme Kyakilika Avatar answered Nov 18 '22 00:11

Buneme Kyakilika


Not really an active issue anymore, but in case someone runs into the same error: Check your app:layout_anchors.

This error just occurred for me when I had a setup similar to this:

<View
    android:id="@+id/firstView"
    app:layout_anchor="@+id/secondView" />

<View
    android:id="@id/secondView"
    app:layout_anchor="@id/firstView" />

Of course the actual case was a bit more complex which was how this slipped in to begin with. Anyway, the error hints at a dependency loop.

like image 20
Justin Hammenga Avatar answered Nov 17 '22 23:11

Justin Hammenga