简体   繁体   English

错误 R.java

[英]Error in R.java

There is an error in R.java in this code on the second line第二行这段代码R.java有错误

 public static final class id {
    public static final int 2ndQ=0x7f05000a;
    public static final int FirstQuestion=0x7f050000;
    public static final int MainTitle=0x7f050003;
    public static final int SecondQuestion=0x7f050008;
    public static final int btnstrt=0x7f050007;
    public static final int editText1=0x7f050001;
    public static final int imageView1=0x7f050006;
    public static final int maindescrip=0x7f050004;
    public static final int radio0=0x7f05000b;
    public static final int radio1=0x7f05000c;
    public static final int radio2=0x7f05000d;
    public static final int radioGroup1=0x7f050009;
    public static final int toq1=0x7f050005;
    public static final int toq2=0x7f050002;
    public static final int toq3=0x7f05000e;

It gives me this error它给了我这个错误

Syntax error on token "2", delete this token标记“2”的语法错误,删除该标记

I try to delete that line as it is the ID for a TextView I no longer have.我尝试删除该行,因为它是我不再拥有的 TextView 的 ID。 Infact, I changed the ID of the TextView to "SecondQuestion" from "2ndQ".事实上,我将 TextView 的 ID 从“2ndQ”更改为“SecondQuestion”。

How do I get rid of the error?我如何摆脱错误? It's preventing me from testing my app.它阻止我测试我的应用程序。

EDIT: Just to clarify this, the id is not present in my project AT ALL.编辑:为了澄清这一点,id 根本不存在于我的项目中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/SecondQuestion"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/secondQ"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:textSize="25dp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/2ndQ"
        android:layout_below="@+id/2ndQ"
        android:layout_marginTop="74dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/q2Answer1" />

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

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/q2Answer3" 
            android:gravity="center" />
    </RadioGroup>

    <Button
        android:id="@+id/toq3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/buttonForQuestion3"
        android:textSize="20dp"
        android:clickable="false" />

</RelativeLayout>

I try to delete that line as it is the ID for a TextView I no longer have我尝试删除该行,因为它是我不再拥有的 TextView 的 ID

Sounds like the R.java file has not updated, try some of these general pointers to begin with:听起来 R.java 文件还没有更新,请尝试从以下一些通用指南开始:

  • Do not manually edit any of the files in the gen folder, as these are automatically built for you.不要手动编辑gen文件夹中的任何文件,因为这些文件是自动为您构建的。
  • If you find it is out of sync then try refreshing the project.如果您发现它不同步,请尝试刷新项目。
  • If that did not work try a clean (project -> clean)如果那不起作用,请尝试清理(项目 -> 清理)
  • If that did not work delete the gen folder, from within Eclipse, (do not panic) it will then be rebuilt by Eclipse - however if there is a coding error of some sort it may not do this straight away.如果这不起作用,请从 Eclipse 中删除gen文件夹(不要惊慌),然后它将由 Eclipse 重建 - 但是,如果存在某种编码错误,它可能不会立即执行此操作。

I had a very similar error message starting off with Android development.从 Android 开发开始,我有一条非常相似的错误消息。 In my case it was a simple naming error on one of the res strings - I had android:id="@+id/edit/message".在我的例子中,这是一个 res 字符串上的简单命名错误 - 我有 android:id="@+id/edit/message"。 The second / should have been an underscore.第二个 / 应该是下划线。

The only error message was in R.java so when starting out with development was very confusing.唯一的错误消息在 R.java 中,因此在开始开发时非常混乱。

After 5 hours, I finally Got to the bottom of this hell, I had this error too, but none of the above mentioned methods helped, apparently i have renamed by mistake an id in a file that belongs to android internal: and not only that - i renamed to sth illegal (@+id/@+id/checkbox_spinner) Using notepad++ search i located the file in c.\Users\.android\build-cache.., I erased the build-cache folder, the next start of AS was naturally slower as it created all the necessary files, and WOALLA! 5 小时后,我终于找到了这个地狱的底部,我也有这个错误,但上面提到的方法都没有帮助,显然我错误地重命名了属于 android internal: 的文件中的 id 不仅如此- 我重命名为 sth illegal (@+id/@+id/checkbox_spinner) 使用 notepad++ 搜索我找到文件在 c.\Users\.android\build-cache..,我删除了 build-cache 文件夹,下次启动AS 的速度自然较慢,因为它创建了所有必要的文件,而且 WOALLA! problem fixed!问题解决了!

Clean and build your application and try to run your application again..Don't do anything in R.java its automatically generated.Goto Project and clean it,build it.And try to run again.清理并构建您的应用程序,然后尝试再次运行您的应用程序。不要在其自动生成的 R.java 中执行任何操作。转到项目并清理它,构建它。然后尝试再次运行。

Make sure the elements in your xml files are not empty.确保 xml 文件中的元素不为空。 The R file doesn't like it when there's an un-use or empty element with no values.当存在未使用或没有值的空元素时,R 文件不喜欢它。 For example:例如:

Line 1: Average Height第 1 行:平均身高

Line 2:第 2 行:

The R file doesn't like it when line 2 has empty values.当第 2 行具有空值时,R 文件不喜欢它。

I had this exact problem too.我也有这个确切的问题。 Where there was an error in my R.id saying to delete the token, but I was unable to remove it as it was no longer defined anywhere in my xml. So I had a在我的 R.id 中有一个错误说要删除令牌,但我无法删除它,因为它不再在我的 xml 中的任何地方定义。所以我有一个

public static final int 2ndQ=0x7f05000a;

in my R.java that I couldn't get rid even if I cleaned my project.在我的 R.java 中,即使我清理了我的项目,我也无法摆脱。

To get rid of it I had to为了摆脱它,我不得不

  • Recreate a TextView with this bad id of '2ndQ'使用“2ndQ”这个错误 ID 重新创建一个 TextView
  • Clean the project and update R.java清理项目并更新 R.java
  • Then delete the sacrificial TextView I had just created然后删除我刚刚创建的牺牲TextView

Voilà, Your R.java should be fixed.瞧,你的 R.java 应该是固定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM