android布局中的循环依赖关系

时间:2014-10-21 03:23:06

标签: android android-layout

我花了最后一小时试图找到我的布局中的循环依赖,但我无法在任何地方找到它。

有人能看出我的问题所在吗? Logcat似乎没有在它抛出错误的地方提供一个确切的行。

布局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="au.gov.nsw.shellharbour.saferroadsshellharbour.Report_a_Hazard"
    android:background="@drawable/background">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_centerInParent="true">

        <RelativeLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="350dp"
                android:id="@+id/CapturedImage"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Choose Existing"
                android:id="@+id/btn_Choose_existing"
                android:layout_below="@+id/CapturedImage"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/view"
                android:background="#0000FF"
                android:textColor="#FFFFFF"/>

            <View
                android:layout_width="10dp"
                android:layout_height="1dp"
                android:id="@+id/view"
                android:layout_centerHorizontal="true" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Take Photo"
                android:id="@+id/btn_take_photo"
                android:layout_below="@+id/CapturedImage"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/view"
                android:background="#0000FF"
                android:textColor="#FFFFFF"/>

            <EditText
                android:layout_width="140dp"
                android:layout_height="wrap_content"
                android:id="@+id/Location_input"
                android:layout_below="@+id/btn_Choose_existing"
                android:layout_alignParentRight="true"
                android:editable="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="2dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Location_label"
                android:text="Name"
                android:layout_alignBaseline="@+id/Location_input"
                android:layout_alignBottom="@+id/Location_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>

            <EditText
                android:layout_width="140dp"
                android:layout_height="wrap_content"
                android:id="@+id/PhoneNumber_input"
                android:editable="true"
                android:layout_below="@+id/Location_input"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/abc_menu_hardkey_panel_holo_light"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/PhoneNumber_label"
                android:text="Phone Number"
                android:layout_alignBaseline="@+id/PhoneNumber_input"
                android:layout_alignBottom="@+id/PhoneNumber_input"
                android:layout_alignRight="@+id/HazardType_Label"
                android:layout_alignEnd="@+id/HazardType_Label"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Hazard_details_label"
                android:text="Hazard Details"
                android:layout_below="@+id/PhoneNumber_input"
                android:layout_centerHorizontal="true"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>

            <EditText
                android:layout_width="140dp"
                android:layout_height="wrap_content"
                android:id="@+id/NumberandStreet_input"
                android:editable="true"
                android:layout_below="@+id/AdditionalInfo_label"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/abc_menu_hardkey_panel_holo_light"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/NumberandStreet_label"
                android:text="Number and Street"
                android:layout_alignBaseline="@+id/NumberandStreet_input"
                android:layout_alignBottom="@+id/NumberandStreet_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>

            <EditText
                android:layout_width="140dp"
                android:layout_height="wrap_content"
                android:id="@+id/Suburb_input"
                android:editable="true"
                android:layout_below="@+id/NumberandStreet_input"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/abc_menu_hardkey_panel_holo_light"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Suburb_Label"
                android:text="Suburb"
                android:layout_alignBaseline="@+id/Suburb_input"
                android:layout_alignBottom="@+id/Suburb_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>

            <EditText
                android:layout_width="140dp"
                android:layout_height="wrap_content"
                android:id="@+id/HazardType_input"
                android:editable="true"
                android:layout_below="@+id/Suburb_input"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/abc_menu_hardkey_panel_holo_light"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/HazardType_Label"
                android:text="Type of Hazard"
                android:layout_alignBaseline="@+id/HazardType_input"
                android:layout_alignBottom="@+id/HazardType_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/AdditionalInfo_label"
                android:text="Additional Information"
                android:layout_below="@+id/HazardType_input"
                android:layout_centerHorizontal="true"
                android:typeface="sans"
                android:textColor="#FFFFFF"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/AdditionalInfo_input"
                android:editable="true"
                android:layout_below="@+id/AdditionalInfo_label"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="4dp"
                android:background="@drawable/abc_menu_hardkey_panel_holo_light"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back"
                android:id="@+id/btn_Back"
                android:layout_below="@+id/AdditionalInfo_input"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/view2"
                android:background="#0000FF"
                android:textColor="#FFFFFF"/>

            <View
                android:layout_width="10dp"
                android:layout_height="1dp"
                android:id="@+id/view2"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/AdditionalInfo_input"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Send Email"
                android:id="@+id/btn_Send_email"
                android:layout_below="@+id/AdditionalInfo_input"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/view2"
                android:background="#0000FF"
                android:textColor="#FFFFFF"/>




        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

logcat的:

10-21 13:39:41.731  29163-29163/au.gov.nsw.shellharbour.saferroadsshellharbour E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
            at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1321)
            at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:316)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:337)
            at android.view.View.measure(View.java:15371)
            at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1466)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.widget.ScrollView.onMeasure(ScrollView.java:388)
            at android.view.View.measure(View.java:15371)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
            at android.view.View.measure(View.java:15371)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15371)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15371)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
            at android.view.View.measure(View.java:15371)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2003)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1242)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1417)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1135)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4635)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4963)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

正如我之前所说,我花了一个多小时试图找到问题,而我仍然在努力寻找它。我唯一能想到的是我的布局定位在某处错了。

2 个答案:

答案 0 :(得分:1)

不知道我之前删除的答案会发生什么:S

问题在于那4个关系

        <EditText
            android:id="@+id/NumberandStreet_input"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/AdditionalInfo_label" />

        <EditText
            android:id="@+id/Suburb_input"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/NumberandStreet_input" />

        <EditText
            android:id="@+id/HazardType_input"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Suburb_input" />

        <TextView
            android:id="@+id/AdditionalInfo_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/HazardType_input"
            android:text="Additional Information" />
  • NumberandStreet_input 低于AdditionalInfo_label
  • AdditionalInfo_label低于HazardType_input
  • HazardType_input低于Suburb_input
  • Suburb_input位于 NumberandStreet_input
  • 下方

我花了很长时间才找到依赖。我强烈建议你整理你的布局,有很多常见的样式属性应该按照样式分组,以使布局更具可读性。如果我是你,我会改写布局,很可能你可以完成相同的布局,而不需要下面这么多,右边,左边等等,这使得当你需要移动东西时很难,因为每个组件位置都依赖于其他

答案 1 :(得分:0)

这里

<TextView 
                android:id="@+id/AdditionalInfo_label"
                android:layout_below="@+id/HazardType_input"

<EditText
                android:id="@+id/HazardType_input"
                android:layout_below="@+id/Suburb_input"

<EditText
                android:id="@+id/Suburb_input"
                 android:layout_below="@+id/NumberandStreet_input"
<EditText
            android:id="@+id/NumberandStreet_input"
            android:layout_below="@+id/AdditionalInfo_label"

1 - &gt; 2 - &gt; 3 - &gt; 4 - &gt; 1按照layout_bellow:D