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



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

MainActivity.java无法识别单选按钮(尽管导入android.widget.RadioGroup;导入android.widget.RadioButton;是吗),所以我清理了这个项目,因为我想更新R.java然后,R.java消失,控制台显示此图像

我尝试过的事情:
重新清理项目
修复项目部分

这是我的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 时,它就起作用了

我想你对你的问题感到困惑,你的logcat说你在导入support-v7-appcompat库时遇到了一些问题,你说没有R.java

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

请按照以下步骤添加库

按照"支持库设置-添加带有资源的库"中的说明进行操作。有一件事可能会带来一些疑问,那就是将库添加到您的应用程序项目中:

(1) 在项目资源管理器中,右键单击项目,然后选择"属性"。(2) 在左侧窗格中选择Android。(3) 在右侧窗格的底部单击"添加"(4) 选择库项目android-support-v7-appcompat,然后单击确定。单击确定关闭属性窗口。

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

然而,如果你和我一样,没有听从安卓团队的建议,建议你总是使用最新的SDK编译你的源代码,你就会得到错误。

出现错误是因为我的项目构建目标是Android 2.3.3,并且至少需要3.0。我当时还不清楚,因为我是专门为Android 2.3.3开发的,而且我使用的是支持库。

好的,我希望这能有所帮助。

R.java没有生成,因为xml文件中存在错误。如果我是你,我会检查xml文件(尤其是布局文件)中的错误,解决它们,然后清理项目。

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

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

只需转到gen文件夹>R.java&删除R.java并清理您的项目。

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

现在我看到了你发布的图片:-所以我认为你也应该检查一下,也右键点击你的项目->构建路径->android检查appcompact-v7库是否被正确引用。检查lib引用是否有绿色勾号

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

android:layout_below="@+id/radioGroup1"

在你的情况下,你删除了一个无线电组,但你没有从编辑文本2中删除id,所以它可能会给你错误

情况2=可能是您在项目中使用了android-support-v7-appcompat.jar文件,所以只需进入lib文件夹,从lib中删除android-ssupport-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>

希望它的作品

根据图片中显示的Docs和Your错误消息,其中一个错误似乎是,您的V7支持库没有以正确的方式包含在内。但这是模糊的,你的错误可能有多种原因。但首先,尝试一下:

  • 请确保包含v4支持和v7 appcompat库(两者)
  • 请确保您已将这两个库下载到您的Android SDK
  • 你必须像这里的文档中指定的那样设置你的构建版本:

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

如果所有这些都完成了,通常图像中显示的错误消息应该会消失。然后,如果还有一些错误消息,您可以继续处理其他错误消息。如果您需要更多帮助,请回复并提出最新问题。

相关内容

最新更新