造成循环依赖的原因是什么?

时间:2013-04-09 20:51:48

标签: java android relativelayout

我已经查了几个小时但是在这段代码中找不到循环引用。我希望有人能帮我找到错误的位置。

有问题的XML文件在这里;

<?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" >

    <LinearLayout android:id="@+id/politics_panel"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/leadership_panel"
        android:background="#66FF99" >

        <Spinner android:id="@+id/politics_spinner"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

        <Button 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/ok"
            android:onClick="addCardToTray" /> 
    </LinearLayout>

    <LinearLayout android:id="@id/leadership_panel"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_toLeftOf="@+id/tactics_panel"
        android:layout_toRightOf="@id/politics_panel"
        android:background="#3333CC">

        <Spinner android:id="@+id/leadership_spinner"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

        <Button 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/ok"
            android:onClick="addCardToTray" />  
    </LinearLayout>

    <LinearLayout android:id="@id/tactics_panel"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_toLeftOf="@+id/piloting_panel"
        android:layout_toRightOf="@id/leadership_panel"
        android:background="#CC3399" >

        <Spinner android:id="@+id/tactics_spinner"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/ok"
            android:onClick="addCardToTray" />
    </LinearLayout>

    <LinearLayout android:id="@id/piloting_panel"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_toLeftOf="@+id/engineering_panel"
        android:layout_toRightOf="@id/tactics_panel"
        android:background="#FFFF00" >

        <Spinner android:id="@+id/piloting_spinner"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/ok"
            android:onClick="addCardToTray" />
    </LinearLayout>

    <LinearLayout android:id="@id/engineering_panel"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/piloting_panel"
        android:background="#990033" >

        <Spinner android:id="@+id/engineering_spinner"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/ok"
            android:onClick="addCardToTray" />
    </LinearLayout>
</RelativeLayout>

运行代码时给出的LogCat打印输出是这样的;

04-09 21:49:32.834: E/AndroidRuntime(17888): FATAL EXCEPTION: main
04-09 21:49:32.834: E/AndroidRuntime(17888): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1321)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:317)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:337)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.View.measure(View.java:15530)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.View.measure(View.java:15530)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.View.measure(View.java:15530)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.View.measure(View.java:15530)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.View.measure(View.java:15530)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1986)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1227)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.Choreographer.doFrame(Choreographer.java:525)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.os.Handler.handleCallback(Handler.java:615)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.os.Looper.loop(Looper.java:137)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at android.app.ActivityThread.main(ActivityThread.java:4921)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-09 21:49:32.834: E/AndroidRuntime(17888):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

leadership_panel表示politics_panel,反之亦然。

答案 1 :(得分:0)

leadership_panelpolitics_panel选择一个位置,而不是将每个位置相对于彼此放置,而不进行实际锚定。