1

我一直在考虑为 Android 开发一段时间,我终于冒险了。Eclipse 设置好了,Android SDK 和 ADT Plugin for eclipse 也设置好了。我在网上找到了一个教程,并按照它的说明进行操作。我无法弄清楚的问题是 Eclipse 使用以下代码生成并出错,主要是 text.setText() 和 text.getText() 调用,它强调了它的文本部分:

我错过了进口吗?

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 testActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

// 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.radioButton1);
        RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radioButton2);
        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

3 回答 3

0

您没有声明 textedit 对象。在您的 onCreate 方法中添加:

text = (EditText) findViewById(R.id.editText1);

editText1 可能不同,具体取决于您的 main.xml 布局文件

还要在 onCreate 方法之前添加:

private EditText text;
于 2012-04-25T18:14:47.223 回答
0

而不是你的代码使用这个,

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 ConvertActivity 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;
    }
}

主要的.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="@color/myColor"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal|numberSigned" >
    </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:onClick="myClickHandler"
        android:text="@string/calc" >
    </Button>

</LinearLayout>
于 2012-04-25T18:16:17.393 回答
0

好像您还没有声明文本,它是 TextView 还是 EditText?

像这样定义它

public class testActivity extends Activity {
/** Called when the activity is first created. */
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView)findViewById(R.id.yourTextViewId);
}

以这种方式声明后,您将看不到错误。

于 2012-04-25T18:16:21.553 回答