Java 这个简单的RelativeLayout Android程序有什么问题?

Java 这个简单的RelativeLayout Android程序有什么问题?,java,android,button,android-relativelayout,android-xml,Java,Android,Button,Android Relativelayout,Android Xml,我创建了这个非常简单的RelativeLayout,作为我以后可能想做的事情的测试。目前,应该有3个按钮,second、alpha和mode相对排列 我有两个问题:首先,XML布局文件有问题,我不知道是什么问题,因为我已经彻底检查了它。这会导致布局的第10行和第11行出现错误。其次,没有生成的R文件,就像android活动中通常存在的那样。这导致主代码的第11行出现错误,因为它试图从R文件调用某些内容 我的代码如下: 布局: <?xml version="1.0" encoding="ut

我创建了这个非常简单的
RelativeLayout
,作为我以后可能想做的事情的测试。目前,应该有3个按钮,
second
alpha
mode
相对排列

我有两个问题:首先,XML布局文件有问题,我不知道是什么问题,因为我已经彻底检查了它。这会导致布局的第10行和第11行出现错误。其次,没有生成的
R
文件,就像android活动中通常存在的那样。这导致主代码的第11行出现错误,因为它试图从R文件调用某些内容

我的代码如下:

布局:

<?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:orientation="vertical" >
    <Button
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/alpha"
        android:layout_toLeftOf="@id/mode"
        android:text="@string/second"
        android:layout_alignParentLeft="true" />
    <Button
        android:id="@+id/alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha" />
    <Button
        android:id="@+id/mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/mode" />
</RelativeLayout>
谢谢你的帮助

在第10行试试这个

android:layout_above="@+id/alpha"
这是11年前的事

android:layout_toLeftOf="@+id/mode"

当xml文件包含错误时,将不会生成R类。您的错误是:

android:layout_above="@id/alpha"
付诸表决:


出现错误的原因是您引用的ID未由文件中的该点定义。只能相对于文件中元素上方定义的ID进行布局


没有
R.java
文件,因为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"
    android:orientation="vertical" >

    <Button
        android:id="@+id/mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha" />

    <Button
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/mode"
        android:text="@string/second" />

    <Button
        android:id="@+id/alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/mode"
        android:layout_below="@id/mode"
        android:text="@string/alpha" />

</RelativeLayout>

您没有得到R.java,因为当XML文件中有错误时,它不会生成


此外,尽管您没有要求这样做,但原始XML文件具有循环依赖关系。这意味着两个小部件相互引用的方式会创建一个无限循环,比如a在B的左边,B在a的右边

听起来你可能应该在android开发者网站上阅读一下,了解android项目是如何工作的。这是错误的,@+id当你声明元素android:id=“@+id”,当您以后使用它时,不使用+访问元素,但如果您在创建它之前引用它,使用“+”将创建它,不是吗?(至少这是我的想法)。我认为,要创建一个元素,操作系统需要知道要创建什么(TextView、LinearLayout、ListView等等)。如果你只是在上面写android:layout_=“@+id/alpha”期望操作系统创建“alpha”元素,那么它应该知道如何创建什么。努诺是对的。你可以引用一个你以后要创建的元素。我引用了,它也可以,我错了。对不起,不知怎的,我确信它不起作用。然而,不管xml是否仍然有效,声明都应该放在第一位。在我的测试中,图形生成器在我的id名称之后发生了变化。就是这样!我不知道在布局中如何声明按钮的顺序很重要。一旦我修复了这个问题并在eclipse中快速清理了项目,一切都按预期进行。再次感谢。读一读马克西姆在我的回答上写的评论:“我做了,它起作用了,我错了。对不起,不知何故,我确信它不起作用。”。
android:layout_above="@+id/alpha"
<?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:orientation="vertical" >

    <Button
        android:id="@+id/mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha" />

    <Button
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/mode"
        android:text="@string/second" />

    <Button
        android:id="@+id/alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/mode"
        android:layout_below="@id/mode"
        android:text="@string/alpha" />

</RelativeLayout>