2013-03-13 103 views
0

也許我的谷歌搜索技巧並不是最大的,但每當我谷歌這個問題,我只找到解決方案,當整個R文件不生成這不是我的問題。其他一切都很好,但是,R內沒有ID,所以我無法在XML中引用視圖。我已經嘗試過project-> clean,然後重新啓動adb服務器和eclipse,但都無濟於事。所有的幫助非常感謝。以下是我的代碼。R.java沒有在Android中生成R.id

public class MainMenu extends Activity { 

    /* 
    * Class member variables 
    */ 
    private Button mMicrophoneButton; 
    private ListView mMessageList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_menu_layout); 
     setWidgets(); 
    } 

    private void setWidgets() { 
     this.mMicrophoneButton = (Button) findViewById(R.id.bMicrophone); 
    } 

} 

下面是我的XML,這裏有很多新的東西,我不承認我已經完成的以前的項目。但我沒有發現任何錯誤,這是我所知道的是,通常像這樣的問題,第一個原因。:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    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=".MainMenu" 
    tools:ignore="HardcodedText" > 

    <ScrollView 
     android:id="@+id/svMessageView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="9" > 

     <TextView 
      android:id="@+id/tvTest" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="test" /> 

    </ScrollView> 

    <Button 
     android:id="@+id/bMicrophone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:minHeight="30dp" 
     android:text="Speak Now" /> 

</LinearLayout> 
+1

可能是你輸入了錯誤的R – Blackbelt 2013-03-13 15:10:47

+0

這是我工作區中唯一的R.我明白你的意思,但我不認爲是這樣。特別是因爲我從來不需要在我的其他android項目中的活動中顯式導入R. Eclipse僅以紅色標出「R.id.bMicrophone」中的「id」。我檢查了R,除了id嵌套類之外,所有內容都在那裏。 – marchese29 2013-03-13 15:15:18

+0

我不知道你在這裏是什麼意思,這是一件新事物嗎?我必須使用R的實際導入語句嗎? Eclipse在MainMenu.java中使用它時會識別R,所以我認爲我不需要。特別是因爲我從來沒有過。 – marchese29 2013-03-13 15:18:00

回答

4
  • 確保所有資源下您的文件被正確建立。即使您的R文件已經生成,如果您有任何錯誤,它也不會更新。
  • 如果您正在引用任何庫項目,那麼如果該庫項目(src或res)中存在任何錯誤,您可能會遇到問題。
  • 在eclipse中做一個項目 - >清理項目及其引用的所有庫項目。
  • 刪除gen文件夾,讓eclipse自動生成它。
+0

是的,如果我沒有弄錯,一切都在正確地建設。我的意思是在res中沒有任何文件中有紅色的x。我在項目中編輯過的唯一文件是我在問題中顯示的文件,我假設所有自動生成的東西都沒有錯誤。 – marchese29 2013-03-13 15:20:36

+0

我編輯了我的答案以添加更多的細節。 – Sababado 2013-03-13 15:25:28

+0

嗯......工作。剛刪除了gen文件夾,然後讓eclipse創建它。像魅力一樣工作。 – marchese29 2013-03-13 15:27:25

0

您需要保存xml文件清理項目之前的ID在R.java

+0

這種類型的答案更適合評論。 – xbello 2014-08-19 22:11:19

0

顯示我已經得到了同樣的錯誤,但我得到了清理和重建項目解決。

實際上,我們也可以手動添加Id。我已經做了幾次。

但是你可以按照以下

  • 打開Android工作室
  • 轉到構建
  • 然後清理項目
  • 步驟後重建項目

解決問題它肯定會工作。