【问题标题】:Android Studio cannot find R.java after accidental Gradle sync意外 Gradle 同步后 Android Studio 找不到 R.java
【发布时间】:2015-02-18 04:30:45
【问题描述】:

在 Android Studio 中工作时,我不小心点击了工具栏中的“将项目与 Gradle 文件同步”按钮。现在 R 的所有实例都以红色突出显示为“无法解析符号 R”。此外,除了目录结构工具栏中的应用程序名称之外的所有文件夹都带有红色下划线,并且我有一条消息说找不到我的一个 XML 布局文件中三个选中的单选按钮的 ID(即使它们是下面几行)。项目结构中的任何地方甚至都没有 R.java。重启 Android Studio 并没有解决这个问题;也没有再次按下相同的按钮。

编辑:我希望能够将我的所有文件复制到一个新项目中,但问题仍然存在于新项目中。

这是我的 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:screenOrientation="landscape"
    tools:context="com.cmpt276.darren.minions.OptionsMenu">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:id="@+id/linearLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num_rows"
            android:id="@+id/textView8"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true" />
        <RadioGroup android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <!--//android:checkedButton="@id/rows3"-->

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rows3"
                android:id="@+id/rows3" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rows4"
                android:id="@+id/rows4" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rows8"
                android:id="@+id/rows8" />
        </RadioGroup>


    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num_minions"
            android:id="@+id/textView9" />
        <RadioGroup android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <!--android:checkedButton="@id/numMinions6"-->

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_6"
            android:id="@+id/numMinions6" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_10"
            android:id="@+id/numMinions10" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_15"
            android:id="@+id/numMinions15" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/minions_20"
            android:id="@+id/numMinions20" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num_columns"
            android:id="@+id/textView10"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <RadioGroup android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="@string/num_columns">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/columns4"
                android:id="@+id/columns4" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/columns6"
                android:id="@+id/columns8" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/columns12"
                android:id="@+id/columns12" />
        </RadioGroup>
    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 清理和构建怎么样?
  • file -> 使缓存无效并重新启动也可以解决问题
  • 如果我的某个布局文件无法编译,我经常会在 R 周围看到一些奇怪的东西。
  • @IllegalArgument 这只会让事情变得更糟。现在我的布局渲染只是纯灰色背景上的微小黑色文本。而且显然有 57 个缺失样式错误。
  • @EMBLEM 我猜nexus手机周围的布局已经消失了。如果没有错误,请尝试编译并运行代码 我有时会看到这种行为,但它并没有阻止我编译是否还有更多错误?

标签: java android r.java-file


【解决方案1】:

参考here

XML 问题将阻止您的 R 文件构建。因此,最好用梳子检查它。在您的代码中

<RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@id/rows3"// error line
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rows3"
            android:id="@+id/rows3" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rows4"
            android:id="@+id/rows4" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rows8"
            android:id="@+id/rows8" />
    </RadioGroup>

错误行当前指的是当前尚未分配的 id。这会在您的 xml 中创建错误,因此 R 不会构建。

This解决方案表示需要将属性提及为

  android:checkedButton="@+id/rows3"//

这应该可以。或者请以编程方式定义它。这样可以避免任何此类问题。

【讨论】:

    【解决方案2】:

    已经在这里回答了这个问题:
    Android Studio: Gradle Build Problems/"R can't be resolved"

    部分答案:

    1. 看,如果您没有在 xml 文件中编写任何代码,那么只需点击 重建或制作项目按钮或清理按钮。你的问题可能是 解决了。​​
    2. 如果您修改 xxxxx.xml 布局文件,请查看这些文件并 我敢肯定你写了一些错误的代码。像主题名称问题, 字符串、尺寸、颜色等的任何引用问题。 更正它们,可能是您的 R 无法解析文本将是 解决了。显然,在执行此操作后单击 make project 按钮和 可证明没有错误..

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      相关资源
      最近更新 更多