R 1不能被解析为一个变量 [英] R cannot be resolved to a variable
问题描述
我想解决这个错误
研究不能被解析为一个变量
我抬头一看很多答案,但我不能得到正确的;我什么都试过。难道任何一个能帮助我吗?
将被自动创建我的主要活动。该错误显示为三线起始于案例R.id.button1:
:
包de.vogella.android.temprature1;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.EditText;
进口android.widget.RadioButton;
进口android.widget.Toast;
公共类转换延伸活动{
私人的EditText文本;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
文=(EditText上)findViewById(R.id.editText1);
}
//此方法被调用时按一下按钮,因为我们分配的名称为
//在单击属性按钮
公共无效myClickHandler(查看视图){
开关(view.getId()){
案例R.id.button1:
单选celsiusButton =(单选)findViewById(R.id.radio0);
单选fahrenheitButton =(单选)findViewById(R.id.radio1);
如果(text.getText()。长度()== 0){
Toast.makeText(这一点,请输入一个有效的数字,
Toast.LENGTH_LONG).show();
返回;
}
浮inputValue将= Float.parseFloat(text.getText()的toString());
如果(celsiusButton.isChecked()){
text.setText(将String.valueOf(convertFahrenheitToCelsius(inputValue将)));
} 其他 {
text.setText(将String.valueOf(convertCelsiusToFahrenheit(inputValue将)));
}
//切换到另一个按钮
如果(fahrenheitButton.isChecked()){
fahrenheitButton.setChecked(假);
celsiusButton.setChecked(真正的);
} 其他 {
fahrenheitButton.setChecked(真正的);
celsiusButton.setChecked(假);
}
打破;
}
}
//转换为摄氏
私人浮动convertFahrenheitToCelsius(浮动华氏){
返回((华氏 - 32)* 5/9);
}
//转换为华氏
私人浮动convertCelsiusToFahrenheit(浮点摄氏度){
返程((摄氏* 9)/ 5)+ 32;
}
}
我的main.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT机器人:背景=@色/ myColor>
< EditText上安卓layout_height =WRAP_CONTENT机器人:ID =@ + ID / editText1
机器人:layout_width =match_parent机器人:inputType =numberDecimal | numberSigned>< /的EditText>
< RadioGroup中的android:layout_height =WRAP_CONTENT机器人:ID =@ + ID / radioGroup1
机器人:layout_width =match_parent>
<单选机器人:layout_width =WRAP_CONTENT
机器人:ID =@ + ID / radio0机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/摄氏度机器人:检查=真>< /单选>
<单选机器人:layout_width =WRAP_CONTENT
机器人:ID =@ + ID /收音机1机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/飞轮海>< /单选>
< / RadioGroup中>
<按钮机器人:ID =@ + ID / button1的机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/钙
机器人:的onClick =myClickHandler>< /按钮>
< / LinearLayout中>
我string.xml
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<字符串名称=你好>的Hello World,转换<!/串>
<字符串名称=APP_NAME> de.vogella.android.temprature1< /串>
<字符串名称=myClickHandler> myClickHandler< /串>
<字符串名称=摄氏>到摄氏< /串>
<字符串名称=华氏>到华氏< /串>
<字符串名称=计算>计算< /串>
< /资源>
1)是否在Package Explorer树中的XML文件有一个错误的图标 如果他们这样做,修正错误,R级将会产生。
例如提示:FILL_PARENT更名MATCH_PARENT在API等级8;所以,如果你的目标平台2.1(API 7),改回所有出现的以FILL_PARENT
2),如果你添加导入android.R;尝试做一个快速的修复,删除该行
I would like to fix this error
R cannot be resolved to a variable
I looked up many answers, but I could not get the right one; I tried everything. Could any one help me?
My main activity which is automatically created. The error is showing for the three lines starting at case R.id.button1:
:
package de.vogella.android.temprature1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class Convert extends Activity {
private EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
} else {
text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
}
// Switch to the other button
if (fahrenheitButton.isChecked()) {
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
} else {
fahrenheitButton.setChecked(true);
celsiusButton.setChecked(false);
}
break;
}
}
// Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/myColor">
<EditText android:layout_height="wrap_content" android:id="@+id/editText1"
android:layout_width="match_parent" android:inputType="numberDecimal|numberSigned"></EditText>
<RadioGroup android:layout_height="wrap_content" android:id="@+id/radioGroup1"
android:layout_width="match_parent">
<RadioButton android:layout_width="wrap_content"
android:id="@+id/radio0" android:layout_height="wrap_content"
android:text="@string/celsius" android:checked="true"></RadioButton>
<RadioButton android:layout_width="wrap_content"
android:id="@+id/radio1" android:layout_height="wrap_content"
android:text="@string/fahrenheit"></RadioButton>
</RadioGroup>
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/calc"
android:onClick="myClickHandler"></Button>
</LinearLayout>
my string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Convert!</string>
<string name="app_name">de.vogella.android.temprature1</string>
<string name="myClickHandler">myClickHandler</string>
<string name="celsius">to Celsius</string>
<string name="farenheit">to Farenheit</string>
<string name="calc">Calculate</string>
</resources>
1) see if the xml files in the Package Explorer tree have an error-icon if they do, fix errors and R-class will be generated.
example hint: FILL_PARENT was renamed MATCH_PARENT in API Level 8; so if you're targeting platform 2.1 (api 7), change all occurences back to "fill_parent"
2) if you added "import android.R;" trying to do a quick-fix, remove that line
这篇关于R 1不能被解析为一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!