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

时间:2013-06-22 02:16:30

标签: android

开发我的第一个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

4 个答案:

答案 0 :(得分:13)

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

编辑:原始答案无效,但清理项目解决了问题。

答案 1 :(得分:0)

我刚遇到这个问题。似乎没有正确编译xml布局文件。或者更确切地说,它不包含在要编译的已更改文件列表中。

答案 2 :(得分:0)

我遇到了同样的情况,但我发现在不同的活动中有两个具有相同ID的文本视图,因此我更改了其中一个并且程序运行清晰,因此检查所有edittext和按钮的ID并更改samilier甚至如果他们在其他活动中,我认为它会在没有任何问题的情况下运行

答案 3 :(得分:0)

我按照答案中的步骤(清理然后确定它是ID)并注意到我EditText R.id的来源将我带到了EditText。认为这绝对不是IDE缓存问题。

我所做的是更改LinearLayout

android:layout_width="fill_parent" 
android:layout_height="fill_parent"

android:layout_width="match_parent"
android:layout_height="match_parent"

出于某种原因,它解决了这个问题(我把这整个布局包装在我刚刚删除的其他东西中)。

相关问题