简体   繁体   English

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

[英]java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

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) java.lang.ClassCastException:android.widget.TextView无法转换为com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)上的android.widget.EditText

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

Your heightIn is not EditText and you're trying to cast it as EditText so you got Error 您的heightIn不是EditText并且您尝试将其heightInEditText因此出现错误

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText at com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32) java.lang.ClassCastException:android.widget.TextView无法转换为com.example.bmicalculator.MainActivity.initializeApp(MainActivity.java.32)上的android.widget.EditText

so do as, 这样做

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

Change this: 更改此:

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

Via 通过

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

OR 要么

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

via 通过

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

in java file 在java文件中

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

and

in xml file 在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" />

So change according to your requirement. 因此,请根据您的要求进行更改。

  1. There are two possibilities, HeightInput1 id component should be Edit text not a text view 有两种可能性,HeightInput1 id组件应该是Edit text不是text view
  2. else In code change the Edit text to Text view whose id is HeightInput1 else在代码中,将“编辑文本”更改为ID为HeightInput1的“文本”视图

so change java code with this 所以用这个改变java代码

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

else change in xml file with this 否则在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" />

In Xml file you are declare as TextView and you are trying to parse TextView to EditText that's why you are getting an Exception. 在Xml文件中,您将声明为TextView,并且尝试将TextView解析为EditText,这就是为什么您获得异常的原因。

Change EditText Variable 更改EditText变量

private EditText heightIn;
private EditText weightIn;

To

private TextView heightIn;
private TextView weightIn;

And replace your initializeApp() function with: 并将您的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);

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

Here you mentioned heightIn as EditTect and in your layout.xml it is as TextView, 在这里,您将heightIn称为EditTect,在layout.xml中将其称为TextView,
instead of finding "HeightInput1" which is just your text to label, it should find the EditText 而不是找到只是要标记的文本“ HeightInput1”,它应该找到EditText

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

same with the weight input... good luck! 与重量输入相同...祝您好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText - Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText android.widget.TextView无法转换为android.widget.EditText 5 - android.widget.TextView cannot be cast to android.widget.EditText 5 java.lang.ClassCastException:android.widget.ScrollView无法强制转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.ImageButton无法转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.widget.TextView - java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView java.lang.ClassCastException: android.widget.EditText - java.lang.ClassCastException: android.widget.EditText java.lang.ClassCastException:android.widget.TextView - java.lang.ClassCastException: android.widget.TextView 我不断收到错误 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView - i keep getting the error java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM