[英]Id cannot be found (Resource Issue)
好的,所以我正在一个项目上,并且尝试在活动中使用带有文本的按钮,但是eclipse找不到id或Chooser.xml
这是myActivity类
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);
}
});
}
}
这是choicer.xml文件
<?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>
这是字符串资源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>
您的gen文件很可能尚未生成。 尝试构建它,它应该使线条消失。
1)您需要导入您的R类。 import <android package>.R
参考: 无法解析R.id并且无法解析 R-Android错误
2)您的R.id.button1
是否应该为R.id.button_1
,以此类推?
我知道这是一篇很老的文章,尽管我在寻找类似问题时仍然偶然发现。 当我构建android项目时,它会自动添加:
import android.R;
当我使用上述建议时,
import <android package>.R;
而且我仍然使用R.id .....,它仍然传播了一个错误。 我删除了
import android.R;
也没有错误。
choicer.xml是否在正确的文件夹(项目随附的默认布局文件夹)中? 您是否曾经尝试去Project并选择Clean? 代码中的一切看起来都还不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.