Android - R 类的问题

标签 android android-layout

我正在尝试在 Android 中编写一些基本的东西(listView 等)。我的问题如下:
1. 我编写的任何资源(例如,指定 listView 或按钮内容的 xml 文件)都会在 R 类中注册,但当我尝试使用它时,eclipse 将其标记为错误。

示例:

相对布局 xml 文件:

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

  <ListView android:id="@android:id/android:list"
     android:textFilterEnabled="true"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:dividerHeight="3dp"
  />

  <TextView android:id="@+id/simpleText"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

  <Button android:id="@+id/nextButton"
     android:text="@string/next"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"
  />

</RelativeLayout>  

代码如下:

public class sample extends ListActivity {
    /** Called when the activity is first created. */

    private Button nxtButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.v(this.toString(), "Inside the function.");

        nxtButton = (Button)findViewById(R.id.nextButton);
        nxtButton.setOnClickListener(new View.OnClickListener() {   
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //This method is to change to the next screen.
                Log.v(this.toString(), "The next button has been clicked. Taking you to the next screen.");             
            }
        });

        }
}  

R.java 看起来像这样:

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int nextButton=0x7f050001;
        public static final int simpleText=0x7f050000;
    }
    public static final class layout {
        public static final int relLayout=0x7f030000;
        public static final int list_item=0x7f030001;
        public static final int main=0x7f030002;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
        public static final int nextButton=0x7f040002;
    }
}  

我的问题如下:
1. 即使在 R.java 中注册了下一个按钮,每次我尝试对我定义的任何资源执行 findViewById 时都会出错。构建项目也不会产生任何结果。

我得到的错误是:nextButton cannot be resolved or is not a field

我在这些方面遇到了一些关于 SO 的问题,但没有一个是我能够成功使用的解决方案。

最佳答案

你应该确保你已经导入了你的 R java 类,而不是 android.R

关于Android - R 类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6306533/

相关文章:

android - 使用 AlarmManager 启动 Activity ,不使用 Broadcastreceiver

android - 如何查找使用 Android 的手机中可用的传感器?

android - Android 中的 anchor View

android - 带有全选复选框的可扩展 ListView : group itemclick and scrolling bug

android - 强制 ScrollView 和/或 RecyclerViewt 始终可滚动

android - Imageview 宽度 match_parent 需要水平居中

android - Android 2.2 库存浏览器中的抗锯齿

Android Studio 3.1 - 插件 Android 支持异常

android - 如何在约束布局中动态设置权重

android - MpAndroidChart 饼图图例在底部中心切割问题