我想解决这个错误
R无法解析为变量
我查了许多答案,但我找不到合适的答案; 我尝试了一切.任何人都可以帮助我吗?
我自动创建的主要活动.从以下三行开始显示错误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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
小智 19
1)查看Package Explorer树中的xml文件是否有错误图标(如果有),修复错误并生成R-class.
示例提示:FILL_PARENT在API级别8中重命名为MATCH_PARENT; 因此,如果您的目标是平台2.1(api 7),请将所有出现的内容更改回"fill_parent"
2)如果你添加了"import android.R;" 试图快速修复,删除该行
and*_*per 16
对我来说,向"R"文件存在问题的人建议的最佳解决方案是尝试后续步骤(顺序无关紧要):
更新ADT和SDK,Eclipse和Java(1.6和最新版本).
编辑:现在ADT支持Java 1.7.链接在这里.
删除gen文件夹,然后重新创建它.
做一个干净的项目.
右键单击该项目,然后选择android-tools - > fix-project-properties.
右键单击该项目,然后选择属性 - > java-build-path - > order-and-export.确保订单是:
Android 4.4(总是最新版本)
Android私人图书馆
android依赖项
你的图书馆项目如果需要的话
yourAppProject/GEN
yourAppProject/src目录
确保res文件夹子文件夹中的所有文件都有正确的名称:只有小写字母,数字和下划线("_").
始终确保targetSdk指向最新的API(当前为19),并将其设置在project.properties文件中
尝试运行Lint并阅读其警告.他们可能会帮助你.
确保你的项目编译1.6 java而不是不同的版本.
重启日食.
小智 5
我有同样的问题.我没有任何说明"import something.R"的行.清洁和重建也没有解决问题.
原来是因为布局xml文件的名称.这些文件只能包含小写字母,数字或下划线.当我将文件名(以及引用它的所有内容)从myLayout.xml更改为my_layout.xml时,一切正常.
| 归档时间: |
|
| 查看次数: |
104801 次 |
| 最近记录: |