为什么R.java消失(R无法解决错误)?

时间:2014-10-30 10:09:27

标签: java android xml android-support-library r.java-file

我知道这是一个非常常见的问题,我读过以前的帖子 但我的项目工作正常,在我添加了一个单选按钮后,它破坏了界面 然后我删除了单选按钮和一些界面内容(如编辑文本和按钮)并再次添加它们。现在接口很好。

MainActivity.java无法识别单选按钮(虽然导入android.widget.RadioGroup; import android.widget.RadioButton;因为我想要更新R.java,所以我清理了项目 然后,R.java DISAPPEARS和控制台显示此image

我尝试过的事情:
 重新清洁项目
 修复项目portieres

这是我的main_activity.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.reg.MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textPersonName" > 
        <requestFocus />
        </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />
    </RadioGroup>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
         android:inputType="textEmailAddress" >


    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText4"
        android:layout_alignLeft="@+id/editText2"
        android:layout_marginBottom="18dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText4"
        android:layout_below="@+id/editText4"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:inputType="date" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText5"
        android:layout_below="@+id/editText5"
        android:layout_marginTop="20dp"
        android:ems="10" 
         android:inputType="textEmailAddress"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp"
         />

</RelativeLayout>

请证明你的答案,我只是一个初学者

更新
当我将目标更改为21但我希望它为20

时,它可以工作

5 个答案:

答案 0 :(得分:0)

只需转到gen文件夹&gt; R.java&amp;删除R.java并清理你的项目。

将自动生成R.java,并删除您的错误。

现在我已经看到你发布的图片了: - 所以我认为你也应该检查一下,也可以右键单击你的项目 - &gt; build path-&gt; android 检查appcompact-v7库是否正确引用。检查lib引用是否有绿色勾号

答案 1 :(得分:0)

由于您的xml文件中存在 错误 ,因此未生成R.java。如果我是你,我会检查xml文件(特别是布局文件)是否有错误,解决它们然后清理项目。

尝试将您的XML文件更改为:(首先定义android命名空间,然后再定义工具)

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    .....

答案 2 :(得分:0)

我认为您对您的问题感到困惑,您的logcat表示您在导入support-v7-appcompat库时遇到了一些问题而且您说,没有R.java

我认为您在添加support-v7-appcompat库时遇到问题,因此可能会影响R.java

请按照以下步骤添加lib

支持库设置中的说明 - 使用资源添加库。 可以带来一些疑问的一件事是将库添加到您的aplicattion项目中:

(1)在Project Explorer中,右键单击项目并选择Properties。 (2)在左侧窗格中选择Android。 (3)在右侧窗格的底部,单击“添加” (4)选择库项目android-support-v7-appcompat,然后单击OK。单击“确定”关闭属性窗口。

如果你没有得到我提到的任何错误,那就完成了。

但是如果你像我一样,你不会听从Android团队的建议,建议你总是使用最新的SDK编译你的源代码,你就会收到错误。

错误发生,因为我的Project Build Target是Android 2.3.3并且需要至少3.0。 我没有立即清楚,因为我专门为Android 2.3.3开发,而且我正在使用支持库。

好的,我希望这有帮助。

答案 3 :(得分:0)

案例1 =我认为你从xml中手动删除了单选按钮,所以当我们从xlm删除textview等上的某个按钮时,其他一些编辑文本或文本视图中包含了id,如下面或上面的选项中所示xml所以R.java文件也有这些id,所以只需要在R.java中找到错误,并从R.java中删除该id,xml也像

机器人:layout_below =&#34; @ + ID / radioGroup1&#34;

在您的情况下,您删除了一个广播组,但是您没有从编辑文本2中删除ID,因此可能会给您带来错误

案例2 =可能是你在项目中使用android-support-v7-appcompat.jar文件这么简单进入lib文件夹并从lib中删除android-support-v4.jar并清除并重建你的项目

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/radioGroup1"
    android:layout_marginTop="24dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
     android:inputType="textEmailAddress" >
</EditText>

希望它的作品

答案 4 :(得分:0)

根据您在图片中显示的文档和您的错误消息,似乎其中一个错误是,您的V7支持库未包含在正确的方式中。但这很模糊,你的错误可能有多种原因。但是对于第一个,尝试一下:

  • 确保您拥有v4支持并包含v7 appcompat库(两者)
  • 请确保您已将这两个库下载到Android SDK
  • 您必须设置您在此处的文档中指定的构建版本:

https://developer.android.com/tools/support-library/features.html#v7

如果完成所有这些操作,通常会删除您在图像中显示的错误消息。然后你可以继续使用其他错误消息,如果还有一些。如果您需要更多帮助并显示更新问题,请返回。

相关问题