0

我收到以下错误:

java.lang.ClassCastException: android.widget.TextView 无法在 com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32) 处转换​​为 android.widget.EditText

主要活动:

public class MainActivity extends Activity {

private EditText heightIn;
private EditText weightIn;
private Button CalculateButton;
private double weight =0;
private double height =0;
private TextView BmiResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeApp();
    }

    private void initializeApp() {
        heightIn = (EditText) findViewById (R.id.HeightInput1);
        weightIn = (EditText) findViewById (R.id.WeightInput1);
        CalculateButton = (Button) findViewById (R.id.CalculateButton);
        BmiResult = (TextView) findViewById (R.id.BmiResult);

    }

    public void calculateBMI(View v) {
        weight = Double.parseDouble(weightIn.getText().toString());
        height = Double.parseDouble(heightIn.getText().toString());
        double bmi = (weight / (height * height));
        String result = String.format("%.2f", bmi);
        Log.d("MyActivity", result);
        BmiResult.setText(result, TextView.BufferType.NORMAL);
    }
}

XML 文件:

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<TextView
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/InchesHint"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/WeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/WeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/PoundsHint"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/CalculateButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="calculateBMI"
    android:text="@string/CalculateButton" />

<TextView
    android:id="@+id/BmiResult"
    android:layout_width="100dp"
    android:layout_height="0dp"
    android:layout_weight="0.00"
    android:text="@string/BmiResult" />

4

6 回答 6

4

heightIn不是EditText,你正试图将它投射,EditText所以你得到了错误

java.lang.ClassCastException: android.widget.TextView 无法在 com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32) 处转换​​为 android.widget.EditText

这样做,

heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);
于 2014-10-30T11:45:01.190 回答
2

在java文件中

heightIn = (EditText) findViewById (R.id.HeightInput1);

在 xml 文件中

<TextView
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

所以根据你的要求改变。

于 2014-10-30T11:46:21.487 回答
2

改变这个:

<TextView
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/WeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/WeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

通过

<EditText
    android:id="@+id/HeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/HeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/WeightInput1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/WeightInput"
    android:textAppearance="?android:attr/textAppearanceMedium" />

或者

heightIn = (EditText) findViewById (R.id.HeightInput1);
weightIn = (EditText) findViewById (R.id.WeightInput1);

通过

heightIn = (TextView) findViewById (R.id.HeightInput1);
weightIn = (TextView) findViewById (R.id.WeightInput1);
于 2014-10-30T11:45:42.930 回答
1
  1. 有两种可能,HeightInput1 id 组件应该是 Edit text 而不是 text view
  2. 否则在代码中将编辑文本更改为 id 为 HeightInput1 的文本视图

所以用这个改变java代码

    heightIn = (TextView) findViewById (R.id.HeightInput1);  
    weightIn = (TextView) findViewById (R.id.WeightInput1);

否则用这个改变xml文件

  <EditText
   android:id="@+id/HeightInput1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/HeightInput"
   android:textAppearance="?android:attr/textAppearanceMedium" />
于 2014-10-30T11:45:50.967 回答
1

在 Xml 文件中,您被声明为 TextView,并且您试图将 TextView 解析为 EditText,这就是您收到异常的原因。

更改 EditText 变量

private EditText heightIn;
private EditText weightIn;

private TextView heightIn;
private TextView weightIn;

并将您的 initializeApp() 函数替换为:

private void initializeApp() {
    heightIn = (TextView) findViewById (R.id.HeightInput1);
    weightIn = (TextView) findViewById (R.id.WeightInput1);
    CalculateButton = (Button) findViewById (R.id.CalculateButton);
    BmiResult = (EditText) findViewById (R.id.BmiResult);

}
于 2014-10-30T11:46:14.040 回答
1
 heightIn = (EditText) findViewById (R.id.HeightInput1);

在这里,您将 heightIn 称为 EditTect,而在您的 layout.xml 中,它是 TextView,
而不是找到“HeightInput1”,它只是您要标记的文本,它应该找到 EditText

 heightIn = (EditText) findViewById (R.id.EditText1);   

与重量输入相同......祝你好运!

于 2014-10-30T11:50:14.933 回答