我基本上是在做一个教程,但我的程序似乎有错误似乎来自R文件。我已经摆脱了自动导入,我的androidManafest.xml也给出了自动生成的错误。我只是想学习构建一个体面的应用程序。任何有关修复错误的有用建议将不胜感激。我指出我的错误与“我得到错误 - >”。
我的convert.java
package de.vogella.android.tempconvertor;
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);
i get error -> setContentView(R.layout.main);
i get error -> text = (EditText) findViewById(R.id.EditText01);
}
// 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()) {
i get error -> case R.id.Button01:
i get error -> RadioButton celsiusButton = (RadioButton) findViewById(R.id.RadioButton01);
i get error -> RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.RadioButton02);
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(convertFahrenheitToCelcius(inputValue)));
} else {
text.setText(String
.valueOf(convertCelciusToFahrenheit(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 celcius
private float convertFahrenheitToCelcius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelciusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
这是我的清单xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.tempconvertor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission />
<application
android:icon="@drawable/ic_launcher"
i get error -> android:label="@string/app_name" >
<activity
android:name=".Convert"
i get error -> android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
首先,确保在res\values\strings.xml
之后,请确保源树中有R文件可用:
[app_root]->gen->de.vogella.android.tempconvertor->R.java
之后,请确保已将R资源文件包含在导入列表
中import de.vogella.android.tempconvertor.R;
答案 1 :(得分:0)
也许res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyApp</string>
</resources>