【问题标题】:java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditTextjava.lang.ClassCastException: android.widget.TextView 不能转换为 android.widget.EditText
【发布时间】:2023-04-10 12:54:01
【问题描述】:

我收到以下错误:

java.lang.ClassCastException: android.widget.TextView 不能被强制转换 到 android.widget.EditText 在 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" />

【问题讨论】:

  • 非常感谢任何人的帮助!
  • 将您的editTexts id更改为HeightInput1和WeightInput1
  • 非常感谢,它阻止了程序崩溃。你知道为什么我的计算不起作用吗?
  • 当你点击'CalculateButton'时,你必须实现ButtonClickListener来执行你的操作
  • CalculateButton = (Button) findViewById (R.id.CalculateButton); CalculateButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { calculateBMI(); } }); } public void calculateBMI() { weight = Double.parseDouble(weightIn.getText().toString());高度 = Double.parseDouble(heightIn.getText().toString());双bmi =(体重/(身高*身高));字符串结果 = String.format("%.2f", bmi); Log.d("我的活动", 结果); BmiResult.setText(result, TextView.BufferType.NORMAL);

标签: java android java.lang.class


【解决方案1】:

您的 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);

【讨论】:

    【解决方案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);
    

    【讨论】:

    • 希望对您有所帮助!
    【解决方案3】:

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

    所以根据你的要求改变。

    【讨论】:

      【解决方案4】:

      在 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】:
         heightIn = (EditText) findViewById (R.id.HeightInput1);
        

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

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

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

        【讨论】:

          【解决方案6】:
          1. 有两种可能,HeightInput1 id 组件应该是编辑文本而不是文本视图
          2. else 在代码中将编辑文本更改为 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" />
          

          【讨论】:

          • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
          • 现在可以了吗,否则您认为还有什么可以做的吗?
          • 是的,这看起来确实是一个不错的答案。 :)
          猜你喜欢
          • 1970-01-01
          • 2021-12-14
          • 2019-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-26
          相关资源
          最近更新 更多