Android - trouble with R class
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.Example:
Relative Layout xml file:
<?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 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 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;
}
}
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 afindViewById
on any resource that I have defined. Building the project also does not yield any results.
The error I get is: nextButton cannot be resolved or is not a field
I have come across a few questions on SO on these lines, but none whose solutions I have been able to use successfully.
You should ensure you've imported your R java class, not android.R
.
Remove any false R imports at the top of your Java classes. If that is not the problem, delete your R file and refresh the project (right-click -> refresh).
精彩评论