0

在运行温度转换器应用程序时...在 TemperatureConverter.java 的以下行中

setContentView(R.layout.main); 
text = (EditText) findViewById(R.id.editText1); 
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0); 
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1); 

我收到以下 4 个错误:

  • main 无法解析或不是字段
  • editText1 无法解析或不是字段
  • radio0 无法解析或不是字段
  • radio1 无法解析或不是字段

请帮我解决这些错误。我将此作为我的作业项目。所以请......下面给出了应用程序的整个源代码。

1.main.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"
android:background="@drawable/myshape"
android:orientation="vertical" > 

<EditText 
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned" > 
<requestFocus/>
</EditText> 

<RadioGroup 
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > 

<RadioButton 
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/celsius" > 
</RadioButton> 

<RadioButton 
android:id="@+id/radio1"
android:layout_width="wrap_content"
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>



3.)  res/drawable/myshape.xml:
    <?xml version="1.0" encoding="UTF-8"?> 
<shape 
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> 
<stroke 
android:width="2dp"
android:color="#FFFFFFFF" /> 
<gradient 
android:endColor="#DDBBBBBB"
android:startColor="#DD777777"
android:angle="90" /> 
<corners 
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" /> 
</shape>

4.) res/values/strings.xml:

<resources> 

<string name="hello_world">Hello world!</string> 
<string name="menu_settings">Settings</string> 
<string name="title_activity_convert_temperture_example">ConvertTempertureExample</string> 
<string name="app_name">Temparature Converter</string> 
<color name="myColor">#3399CC</color> 
<string name="myClickHandler">myClickHandler</string> 
<string name="celsius">to Celsius</string> 
<string name="fahrenheit">to Fahrenheit</string> 
<string name="calc">Calculate</string> 

</resources>



5.) TemperatureConverter.java file 
package com.example.TemperatureConverter; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.RadioButton;

import android.widget.RadioGroup; 
import android.widget.Toast; 
   import android.R;
public class TempertureConverter 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))); 
celsiusButton.setChecked(false); 
fahrenheitButton.setChecked(true); 
} else { 
text.setText(String 
.valueOf(convertCelsiusToFahrenheit(inputValue))); 
fahrenheitButton.setChecked(false); 
celsiusButton.setChecked(true); 
} 
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; 
} 
}
4

1 回答 1

2

您正在导入错误的 R 文件:

import android.R;

从您的代码中删除上述行并将其替换为您项目的 R 文件的导入(可能是 com.example.TemperatureConverter.R)

于 2013-02-01T17:22:47.673 回答