Android的 - 麻烦有R级 [英] Android - trouble with R class

查看:120
本文介绍了Android的 - 麻烦有R级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图code在Android的一些基本的东西(的ListView等)。我的问题如下:结果
1.我写的任何资源(例如,一个XML文件中指定的ListView或按钮的内容),被登记在R类,但日食标志它作为一个错误,当我尝试使用它。

I am trying to code some basic stuff (listView and the like) in Android. My problem is as follows:
1. Any resource that I write (eg. an xml file specifying the content of a listView or a button), gets registered in the R class but eclipse flags it as an error when I try to use it.

例如:

相对布局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>  

code是如下:

Code is as follows:

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看起来像这样:

R.java looks like so:

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 上,我已定义的任何资源的时间。建设该项目也不会产生任何结果。

My problem is as follows:
1. Even though the next button is registered in R.java, I get an error every time I try to do a findViewById on any resource that I have defined. Building the project also does not yield any results.

我得到的错误是: Next按钮不能得到解决或不是一个字段

我所遇到的一些问题上等等这些线路,但没有他们的解决方案,我已经能够成功地使用。

I have come across a few questions on SO on these lines, but none whose solutions I have been able to use successfully.

推荐答案

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

You should ensure you've imported your R java class, not android.R.

这篇关于Android的 - 麻烦有R级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆