Okay so I'm working on a project and I'm trying to use buttons with text inside of them in an activity, but eclipse can't find the id or chooser.xml
Here's myActivity class
package com.xxxxxxx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.*;
public class MyActivity extends Activity {`
private Button mButton1;
private Button mButton2;
private Button mButton3;
private Button mButton4;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//chooser is underlined (chooser cannot be resolved or is not a field)
setContentView(R.layout.chooser);
//all ids are underlined(id cannot be resolved or is not a field)
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button4);
mButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity2.class);
startActivity(myIntent);
}
});
mButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activty3.class);
startActivity(myIntent);
}
});
mButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity4.class);
startActivity(myIntent);
}
});
mButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MyActivity.this, Activity5.class);
startActivity(myIntent);
}
});
}
}
Here's the chooser.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/button_1" />
<Button
android:text="@string/button_2"
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_3"
/>
<Button
android:id="@+id/button_4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/button_4"
/>
</LinearLayout>
And here's the string resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test App</string>
<string name="test_image">test.png</string>
<string name="button_1">button_1</string>
<string name="button_2">button_2</string>
<string name="button_3">button_3</string>
<string name="button_4">button_4</string> 开发者_开发技巧
</resources>
Your gen files most likely aren't generated yet. Try building it, it should make the lines go away.
1) You need to import your R class. import <android package>.R
Ref: R.id cannot be resolved AND R cannot be resolved - Android error
2) Aren't your R.id.button1
supposed to be R.id.button_1
and so forth for other buttons?
Is chooser.xml in the correct folder (the default layout folder provided with the project)? Have you perhaps tried to go to Project and choose Clean? Everything looks okay in your code.
I know this is a very old post, though I still stumbled upon it in my search for a similar problem. When I built the android project, it automatically added:
import android.R;
When I used the advice above,
import <android package>.R;
and I still used R.id....., it still propagated an error. I removed
import android.R;
and no errors.
精彩评论