android.widget.Button无法转换为android.widget.EditText。

7

开发我的第一个Android计算器应用程序时,我成功地通过传递意图来更新新活动中的TextView,但这需要用户点击“返回”才能进行另一个计算。我试图使doCalculation按钮更新MainActivity中的简单TextView,并出现以下错误:

06-22 11:08:17.318: E / AndroidRuntime(31328):Caused by: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);
}

更新,以下是参考的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!" />

感谢您的帮助, -迈克尔
4个回答

12

看起来 R.id.number1 或者 R.id.number2 是一个按钮。请检查你的 XML 文件,确保它是一个 EditText。

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


感谢您的快速回复。确认两者都是EditText。 - Michael
5
尝试另一件事情:清理您的项目。 - Oleksiy
1
清理项目解决了问题!谢谢!! - Michael
我试着为你的回答点赞,但是我的声望还不够高。无论如何,再次感谢。 - Michael
我按照答案中的步骤(清理并确保它是id)进行操作,并注意到转到我的EditText R.id的源代码会将我带到EditText。虽然这绝对不是IDE缓存问题。我所做的是更改LinearLayoutandroid:layout_width="fill_parent" android:layout_height="fill_parent"android:layout_width="match_parent" android:layout_height="match_parent"由于某种原因,它解决了这个问题(我将整个布局包装在其他东西中,最近才删除)。 - D.Ginzbourg

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"

由于某种原因,它解决了问题(我将整个布局包装在其他东西中,最近才删除)。


0
我曾经遇到过同样的情况,但后来发现在不同的活动中有两个具有相同ID的TextView,所以我改变了其中一个,程序就可以正常运行了。因此,请检查所有EditText和Button的ID,并更改相似的ID,即使它们位于其他活动中,我认为这样做就不会出现任何问题了。

0
我刚遇到了这个问题。看起来XML布局文件没有被正确编译。或者说它没有被包含在要编译的更改文件列表中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接