簡體   English   中英

錯誤 R.java

[英]Error in R.java

第二行這段代碼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;

它給了我這個錯誤

標記“2”的語法錯誤,刪除該標記

我嘗試刪除該行,因為它是我不再擁有的 TextView 的 ID。 事實上,我將 TextView 的 ID 從“2ndQ”更改為“SecondQuestion”。

我如何擺脫錯誤? 它阻止我測試我的應用程序。

編輯:為了澄清這一點,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>

我嘗試刪除該行,因為它是我不再擁有的 TextView 的 ID

聽起來 R.java 文件還沒有更新,請嘗試從以下一些通用指南開始:

  • 不要手動編輯gen文件夾中的任何文件,因為這些文件是自動為您構建的。
  • 如果您發現它不同步,請嘗試刷新項目。
  • 如果那不起作用,請嘗試清理(項目 -> 清理)
  • 如果這不起作用,請從 Eclipse 中刪除gen文件夾(不要驚慌),然后它將由 Eclipse 重建 - 但是,如果存在某種編碼錯誤,它可能不會立即執行此操作。

從 Android 開發開始,我有一條非常相似的錯誤消息。 在我的例子中,這是一個 res 字符串上的簡單命名錯誤 - 我有 android:id="@+id/edit/message"。 第二個 / 應該是下划線。

唯一的錯誤消息在 R.java 中,因此在開始開發時非常混亂。

5 小時后,我終於找到了這個地獄的底部,我也有這個錯誤,但上面提到的方法都沒有幫助,顯然我錯誤地重命名了屬於 android internal: 的文件中的 id 不僅如此- 我重命名為 sth illegal (@+id/@+id/checkbox_spinner) 使用 notepad++ 搜索我找到文件在 c.\Users\.android\build-cache..,我刪除了 build-cache 文件夾,下次啟動AS 的速度自然較慢,因為它創建了所有必要的文件,而且 WOALLA! 問題解決了!

清理並構建您的應用程序,然后嘗試再次運行您的應用程序。不要在其自動生成的 R.java 中執行任何操作。轉到項目並清理它,構建它。然后嘗試再次運行。

確保 xml 文件中的元素不為空。 當存在未使用或沒有值的空元素時,R 文件不喜歡它。 例如:

第 1 行:平均身高

第 2 行:

當第 2 行具有空值時,R 文件不喜歡它。

我也有這個確切的問題。 在我的 R.id 中有一個錯誤說要刪除令牌,但我無法刪除它,因為它不再在我的 xml 中的任何地方定義。所以我有一個

public static final int 2ndQ=0x7f05000a;

在我的 R.java 中,即使我清理了我的項目,我也無法擺脫。

為了擺脫它,我不得不

  • 使用“2ndQ”這個錯誤 ID 重新創建一個 TextView
  • 清理項目並更新 R.java
  • 然后刪除我剛剛創建的犧牲TextView

瞧,你的 R.java 應該是固定的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM