android.widget.Button无法强制转换为android.widget.EditText

开发我的第一个 Android计算器应用程序,我成功通过意图传递答案来更新新活动中的TextView,但这需要用户点击返回执行另一个计算.我正在尝试使doCalculation按钮更新MainActivity中的简单TextView并获取错误

06-22 11:08:17.318:E / AndroidRuntime(31328):引起:java.lang.classCastException:android.widget.Button无法强制转换为android.widget.EditText

这是我的代码

/** Called when the user clicks the Calculate! button */
public void doCalculation(View view) {
    // Do something in response to button
    int answerInt;
    String answer;
    EditText numberOne = (EditText) findViewById(R.id.number1);
    EditText numberTwo = (EditText) findViewById(R.id.number2);
    int numberOnee = Integer.parseInt(numberOne.getText().toString());
    int numberTwoo = Integer.parseInt(numberTwo.getText().toString());
    answerInt = numberOnee * numberTwoo;
    answer = Integer.toString(answerInt);

    TextView homeAnswerView = (TextView) findViewById(R.id.homeAnswerView);
    homeAnswerView.setTextSize(40);
    homeAnswerView.setText(answer);
}

作为参考,这里是成功启动新活动的代码

// Called when the user clicks the Calculate! button
public void doCalculation(View view) {
    // Do something in response to button
    int answerInt;
    String answer;
    Intent intent = new Intent(this,displayCalculationActivity.class);
    EditText numberOne = (EditText) findViewById(R.id.number1);
    EditText numberTwo = (EditText) findViewById(R.id.number2);
    int numberOnee = Integer.parseInt(numberOne.getText().toString());
    int numberTwoo = Integer.parseInt(numberTwo.getText().toString());
    answerInt = numberOnee * numberTwoo;
    answer = Integer.toString(answerInt);
    intent.putExtra(EXTRA_MESSAGE,answer);
    startActivity(intent);
}

UPDATE,XML供参考:

<EditText
    android:id="@+id/number2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number" />

<EditText
    android:id="@+id/number1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number"
    android:singleLine="true" />

<Button
    android:id="@+id/calculateBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/number2"
    android:layout_alignRight="@+id/number2"
    android:layout_below="@+id/number2"
    android:layout_marginTop="14dp"
    android:onClick="doCalculation"
    android:text="Calculate!" />

谢谢您的帮助,
-Michael

解决方法

似乎R.id.number1或R.id.number2是一个Button.检查您的XML并确保它是EditText.

编辑:原始答案不起作用,但清理项目解决了问题.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...