0

我基本上是在做一个教程,但我的程序似乎有似乎来自 R 文件的错误。我已经摆脱了自动导入,我的 androidManafest.xml 也给出了自动生成的错误。我只是想学习构建一个像样的应用程序。任何有关修复错误的有用建议将不胜感激。我指出了我对此有错误的行“我得到错误->”。

我的转换.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>
4

2 回答 2

1

首先,确保您的 MyApp 字符串res\values\strings.xml

之后,确保您的源代码树中有可用的 R 文件: [app_root]->gen->de.vogella.android.tempconvertor->R.java

之后,确保您已将 R 资源文件包含在导入列表中

import de.vogella.android.tempconvertor.R;
于 2012-03-20T22:47:24.840 回答
0

也许没有“app_name”res\values\strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyApp</string>
</resources>
于 2012-03-20T22:30:57.920 回答