java.lang.ClassCastException:android.widget.TextView不能转换为android.widget.EditText [英] java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

查看:399
本文介绍了java.lang.ClassCastException:android.widget.TextView不能转换为android.widget.EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

  

java.lang.ClassCastException:android.widget.TextView无法施放   到android.widget.EditText在   com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

主要活动:

 公共类MainActivity延伸活动{

私人的EditText heightIn;
私人的EditText加权的;
私人按钮CalculateButton;
私人双人体重= 0;
私人双高= 0;
私人TextView的BmiResult;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        initializeApp();
    }

    私人无效initializeApp(){
        heightIn =(EditText上)findViewById(R.id.HeightInput1);
        加权的=(的EditText)findViewById(R.id.WeightInput1);
        CalculateButton =(按钮)findViewById(R.id.C​​alculateButton);
        BmiResult =(TextView中)findViewById(R.id.BmiResult);

    }

    公共无效calculateBMI(视图v){
        重量= Double.parseDouble(weightIn.getText()的toString());
        身高= Double.parseDouble(heightIn.getText()的toString());
        双BMI =(体重/(身高×高));
        字符串结果=的String.Format(%2F,BMI);
        Log.d(MyActivity,结果);
        BmiResult.setText(结果,TextView.BufferType.NORMAL);
    }
}
 

XML文件:

 < LinearLayout中的xmlns:工具=htt​​p://schemas.android.com/tool​​s
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / LinearLayout2
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
工具:上下文=$ {relativePackage} $ {activityClass}。>

<的TextView
    机器人:ID =@ + ID / HeightInput1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ HeightInput
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的EditText
    机器人:ID =@ + ID / editText1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:EMS =10
    机器人:提示=@字符串/ InchesHint
    机器人:inputType =numberDecimal>

    <不是requestFocus />
< /的EditText>

<的TextView
    机器人:ID =@ + ID / WeightInput1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@字符串/ WeightInput
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的EditText
    机器人:ID =@ + ID / editText2
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:EMS =10
    机器人:提示=@字符串/ PoundsHint
    机器人:inputType =numberDecimal/>

<按钮
    机器人:ID =@ + ID / CalculateButton
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:的onClick =calculateBMI
    机器人:文本=@字符串/ CalculateButton/>

<的TextView
    机器人:ID =@ + ID / BmiResult
    机器人:layout_width =100dp
    机器人:layout_height =0dp
    机器人:layout_weight =0.00
    机器人:文本=@字符串/ BmiResult/>
 

解决方案

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);
加权的=(的TextView)findViewById(R.id.WeightInput1);
 

I'm getting the following Error:

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText at com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

Main Activity:

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 File:

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

解决方案

Your heightIn is not EditText and you're trying to cast it as EditText so you got Error

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText at com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)

so do as,

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

这篇关于java.lang.ClassCastException:android.widget.TextView不能转换为android.widget.EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆