安卓闪退:java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

今天在测试app的时候出现了闪退情况,经过好一番折腾才弄好,记下来以免忘记。

运行时出现闪退的情况,打开logcat发现如下Error

java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
        at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1789)
        at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
 。。。。。。。。。。。。等等一长串

总的只需要看第一句话,说是什么循环依赖不应该存在于相对布局中,哇靠,翻译一下简直秒懂好不,肯定是哪里有了循环约束,翻一下.xml文件发现,果不其然:

安卓闪退:java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

这个惨痛的教训告诉我们:使用相对布局的时候一定不能添加循环约束条件,因为相对布局是根据父容器或者其他控件的位置来进行定位的,也就是说它是以其他控件或父容器为参照物来确定控件位置的,所以当然不能两个控件互相指定对方为参照物啦。