1

我确信这很简单,因为它是我的第一个带有代码的 android 应用程序(我所做的 hello world 示例只是为 XML 中的字符串分配一个值)。我的问题是当试图在我的变量中获取我的按钮的引用时,没有定义 R.id 中的 id?

编译器错误出现在下面代码的注释中:

package com.geeksonhugs.simplecalc;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity
{
    private Button AddButton;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AddButton = (Button)findViewById(R.id.btnAdd);
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'"

        //AddButton.setOnClickListener(this);

    }   

}

XML 布局文件:

<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LayoutRoot" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical">

<TextView 
    android:id="@+id/txtFirstNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strFirstNum" />

<EditText 
    android:id="@+id/edtFirstNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" />

<TextView 
    android:id="@+id/txtSecondNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strSecondNum" />

<EditText 
    android:id="@+id/edtSecondNum" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" />

<Button 
    android:id="@+id/btnAdd" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="@string/strAdd" 
    android:gravity="center" />

<TextView 
    android:id="@+id/txtResult" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="" />

</LinearLayout>
4

5 回答 5

2

I resolved the issue by deleting the bin folder and recompiling using the AIDE App.

于 2013-01-28T06:30:49.927 回答
1

该类R是在您构建项目时由 Android 构建工具为您生成的代码。这意味着有四种可能性:

  1. 您还没有尝试构建项目,在这种情况下,请尝试。

  2. 由于某种原因,Eclipse 认为它不需要构建项目——尝试从 Eclipse 菜单中的 Project > Clean (当然,只有在使用 Eclipse 时才相关)。

  3. 您的清单或您的资源之一中存在一些阻止R构建的错误。应该有 Eclipse 错误指示器。

  4. 您的布局中没有android:id属性,因此没有R.id可用的属性。

于 2012-04-18T23:04:40.323 回答
1

请确保您粘贴的 xml 文件名为 main.xml 并位于 layout 文件夹下。并尝试再次生成 R 文件。这可能会有所帮助。

于 2012-04-18T23:16:40.887 回答
1

对于那些使用 AIDE 引用 Id 时遇到问题的用户,请尝试删除 bin 文件夹中的“gen 文件夹”并重新编译。问题是 R.java 类没有创建一个名为 Id 的构造函数。所以删除包含 R.java 类的 gen 文件夹并重新编译解决了这个问题。

于 2016-03-16T11:50:29.210 回答
0

您必须将id添加到 XML 文件中的组件。像这样:

android:id="@+id/buton1"

现在您可以单击 mainActivity.java 并编写代码“R.id”。你看到提示写“button1”并且没有“id”错误

于 2019-04-01T16:06:16.510 回答