java.lang.ClassCastException:android.widget.TextView无法强制转换为android.widget.EditText

时间:2014-10-30 11:43:05

标签: java android java.lang.class

我收到以下错误:

  

java.lang.ClassCastException:无法转换android.widget.TextView   to android.widget.EditText at   com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

主要活动:

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" />

6 个答案:

答案 0 :(得分:4)

您的heightIn不是EditText而您正试图将其投放为EditText,因此您收到了错误

  

java.lang.ClassCastException:android.widget.TextView不能   强制转换为android.widget.EditText   com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

这样做,

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

答案 1 :(得分: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);

答案 2 :(得分: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" />

根据您的要求改变。

答案 3 :(得分:1)

  1. 有两种可能,HeightInput1 id组件应该是Edit text而不是text view
  2. else在代码中将编辑文本更改为文本视图,其ID为HeightInput1
  3. 如此改变 用这个

    的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" />
    

答案 4 :(得分: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);

}

答案 5 :(得分:1)

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

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

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

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