为什么出现CircularDependencyException异常?

4

嗨,我正在尝试为我的文件上传通知制作通知。这是我的XML:但它一直给我错误提示:RelativeLayout中不能存在循环依赖项。我做错了什么?我没有看到任何问题?我还试图使它看起来类似于浏览器下载器。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="5dp">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/left"
        android:layout_toLeftOf="@+id/right">

        <ImageView 
            android:id="@+id/status_icon"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true" 
        />

        <TextView 
            android:id="@+id/progress_text" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_below="@+id/status_icon"
            android:text="0%"
        />


    </RelativeLayout>

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/right"
        android:layout_toRightOf="@+id/left">

        <TextView 
            android:id="@+id/status_text" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true" 
        />

        <ProgressBar 
            android:id="@+id/status_progress"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@id/status_text"
            android:progressDrawable="@android:drawable/progress_horizontal"
            android:indeterminate="false" 
            android:indeterminateOnly="false" 
        />
    </RelativeLayout>
</RelativeLayout>
3个回答

9

相对布局“left”试图以相对布局“right”的位置进行布局。这是不可能的,因为正如异常所说,它是循环的。如果那辆车还在移动,你怎么能根据另一辆车的停车来停车呢?

根据您的xml文件,我建议您使用LinearLayout。在这里,您所做的任何事情都可以通过两个LL和父RelativeLayout解决。


3

删除 android:layout_toLeftOf="@+id/right" 这行代码。你声明了左侧应该在右侧的左边,然后又声明右侧应该在左侧的右边。这就是一个循环依赖,因为它不知道先布局哪个元素。


0

你在布局中试图相互引用两个布局。代码的一部分如下所示:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout
        android:id="@+id/left"
        android:layout_toLeftOf="@+id/right" />
    <RelativeLayout
        android:id="@+id/right"
        android:layout_toRightOf="@+id/left" />
 </RelativeLayout>

这将导致循环依赖异常。

如何解决?

设置一个布局的重力类型,其他布局引用它。

对于这种情况,请删除android:layout_toRightOf="@+id/left"

因为默认布局重力是左边,所以删除这行。就可以了。

玩得开心 @.@


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接