这是我的 android 应用程序的代码,它不完整
package com.example;
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 MainActivity extends Activity {
private EditText text;
private EditText text2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
text2=(EditText)findViewById(R.id.result);
}
// This method is called at button click because we assigned the name to the
// "OnClick property" of the button
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
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()) {
text2.setText(String
.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(true);
} else {
text2.setText(String
.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
if(KelvinButton.isChecked())
{
text2.setText(String.valueOf(convertoKelvin(inputValue)));
}
}
break;
case R.id.button2:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
float inputValue = Float.parseFloat(text.getText().toString());
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
if (fahrenheitButton.isChecked()) {
text2.setText(String
.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(true);
} else {
text2.setText(String
.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(true);
if(KelvinButton.isChecked())
{
text2.setText(String.valueOf(convertoKelvin(inputValue)));
}
}
break;
case R.id.button3:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
RadioButton KelvinButton=(RadioButton) findViewById(R.id.radio2);
float inputValue = Float.parseFloat(text.getText().toString());
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
if (KelvinButton.isChecked()) {
text2.setText(String
.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(true);
} else {
text2.setText(String
.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
if(KelvinButton.isChecked())
{
text2.setText(String.valueOf(convertoKelvin(inputValue)));
}
}
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;
}
private float convertoKelvin(float celsius)
{
return ((celsius+273)) ;
}
}
正如你所看到的,我每次都定义了我的变量。如果我不这样做,我会得到错误“值可能没有被初始化。我做错了什么。
如果您想知道,我的代码不完整。我很讨厌这个错误,一旦解决这个问题就会继续