错误Circular dependencies cannot exist in RelativeLayout
Dependencies
A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues.
项目地址:https://gitcode.com/gh_mirrors/de/Dependencies
·
问题描述
日前在检查crash日志时发现了如下的错误:
java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
问题分析
错误日志提示是在RelativeLayout中存在了循环依赖导致的。
所谓的循环依赖其实就是指的互相定位。在RelativeLayout这一相对布局中,我们需要设定一个View针对另外的View的相对位置,也就是需要至少一个View作为基准被另外的View进行相对位置设定,如果两个View互为基准View,系统就难以去定位两个View的位置,就会报错。
举个例子进行说明:
存在两个View:ViewA和ViewB,需要设定ViewB在ViewA的下方,那我们只需要在ViewB里面添加layout_below:ViewA即可,如果采用如下的写法,设置ViewA在ViewB的上方,同时设置ViewB在ViewA的下方,就会造成循环依赖,导致系统错误:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:textSize="30sp"
android:background="@color/cardview_light_background"
android:id="@+id/btn1_test_fragment"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_above="@+id/tv_test1"
android:text="这里是新UI" />
<TextView
android:layout_below="@+id/btn1_test_fragment"
android:id="@+id/tv_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
如上方代码同时设置两个View互相以对方为基准View就会报错。
解决办法
只需要破除循环依赖状态即可,使得View之间不互相定位依赖即可。例如上述代码中只需要去掉layout_above或者layout_below属性即可。
A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues.
最近提交(Master分支:4 个月前 )
1997a400 - 4 年前
2f423539 - 4 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)