Eclipse布局编辑器缺少错误

时间:2011-12-12 12:48:50

标签: android eclipse layout

我花了两个小时与一个愚蠢的错字作斗争。我创建了一个类似于此的RelativeLayout:

<RelativeLayout ...>
    <EditView ...
          android:layout_toLeftOf="@+id/button1"/>
    <Button ...
          android:id="@+id/buton1"
          android:layout_toLeftOf="@+id/button2"/>
    <Button ...
          android:id="@+id/buton2"/>
</RelativeLayout>

注意拼写错误的控件名称 - buton1和buton2。

Eclipse没有报告任何类型的错误,它只是无法正确布局控件。更糟糕的是,在模拟器中运行活动也不会导致错误,只是一个错误的布局。

有没有办法让Eclipse或模拟器报告某种“缺少ID buton1”错误?追踪是一个非常令人沮丧的问题 - 一个很容易被计算机检测到的问题。

伊泰。

3 个答案:

答案 0 :(得分:1)

ADT 17将有一个新的lint规则来检测此错误。 (有关ADT 16 lint规则的信息,请参阅http://tools.android.com/tips/lint;有一个重复的ID检测器,但没有一个检测您的方案。)

答案 1 :(得分:0)

如果您编写以加号开头的ID,则会创建ID(如果该ID不存在)。 如果省略ID中的加号,则需要在此之前定义ID。

因此,在您的情况下,如果您编写如下,您将收到“缺少参考”错误:

<RelativeLayout ...>
    <Button ...
          android:id="@+id/buton2"/>
    <Button ...
          android:id="@+id/buton1"
          android:layout_toLeftOf="@id/button2"/>
    <EditView ...
          android:layout_toLeftOf="@id/button1"/>
</RelativeLayout>

答案 2 :(得分:0)

在按钮1中,您可以使用布局属性中的+符号来定义ID

<Button ...
          android:id="@+id/buton1"
          android:layout_toLeftOf="@+id/button2"/>

如上所述,您需要从layout_toLeftOf属性值中删除“+”符号

<Button ...
          android:id="@+id/buton1"
          android:layout_toLeftOf="@id/button2"/>

还有一件事可能会给你错误,因为虽然解释这个xml的时间,但是找不到button2可能就是在这种情况下只需按下button2定义上面的button1同样的方式进入EditView