R.java在Android中没有生成R.id

时间:2013-03-13 15:01:45

标签: android android-xml r.java-file

也许我的谷歌搜索技能不是最好的,但每当我谷歌这个问题,我只找到解决方案,当没有生成整个R文件,这不是我的问题。其他一切都很好,但R中没有ID,所以我无法在XML中引用视图。我已经尝试过project-> clean,并重新启动adb服务器和eclipse,但都无济于事。非常感谢所有帮助。以下是我的代码。

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>

3 个答案:

答案 0 :(得分:4)

  • 确保res下的所有文件都正常构建。即使您的R文件已经生成,如果您有任何错误也不会更新。
  • 如果您正在引用任何库项目,那么如果该库项目(src或res)中存在任何错误,则可能会出现问题。
  • 在eclipse中执行一个项目 - &gt;清理项目及其引用的所有库项目。
  • 删除gen文件夹,让eclipse自动生成它。

答案 1 :(得分:0)

在清理项目之前需要保存xml文件,以便在R.java中显示ID

答案 2 :(得分:0)

我有同样的错误,但我通过清理和重建项目解决了这个问题。

实际上,我们也可以手动添加Id。我做了几次。

但您可以按照以下步骤解决问题

  • 打开Android Studio
  • 转到Build
  • 然后清理项目
  • 在重建项目之后

它肯定会有用。

相关问题