android.widget.Button不能转换为android.widget.EditText转换为、widget、android、EditText

2023-09-06 09:37:40 作者:ㄍ安于此生

开发我的第一个Android的计算器应用程序,我成功地将通过意图传递答案更新了新的活动一个TextView,但是这需要用户打回进行另一次计算。我试图让doCalculation按钮更新在MainActivity一个简单的TextView并且得到错误:

Developing my first Android calculator application, I succeeded in updating a TextView in a new activity by passing the answer via an intent, but this requires the user to hit Back to perform another calculation. I'm trying to make the doCalculation button update a simple TextView in the MainActivity and getting the error:

11月六号至22号:08:17.318:E / AndroidRuntime(31328):java.lang.ClassCastException:产生的原因android.widget.Button不能转换为android.widget.EditText

06-22 11:08:17.318: E/AndroidRuntime(31328): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText

下面是我的code:

/** 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);
}

有关参考,下面的工作成功启动一个新的活动的code:

For reference, here's the code that worked successfully launching a new activity:

// 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,以供参考:

UPDATE, the XML for reference:

<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

Thank you for your help, -Michael

推荐答案

好像无论R.id.number1或R.id.number2是一个按钮。检查你的XML,并确保它是一个EditText。

It seems like either R.id.number1 or R.id.number2 is a Button. Check your XML and make sure it's an EditText.

编辑:原来的答复没有工作,但清理的项目解决了这个问题。

Original answer didn't work, but cleaning the project solved the problem.

 
精彩推荐
图片推荐