cannot be cast to android.widget.edittext,android.widget.Button cannot be cast to android.widget.Edi...

问题

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:

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

Here's my 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);

}

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

}

UPDATE, the XML for reference:

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

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

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

Thank you for your help,

-Michael

回答1:

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

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

回答2:

I've just had this problem. It seems that the xml layout file is not compiled properly. Or rather it is not included in the list of changed files to be compiled.

回答3:

i was having the same situation but i found out that there are two textviews with same ids in different activities so i changed one of them and the program ran clearly so check the ids of all your edittext and buttons and change the samilier even if they were in other activities and i think it will run with out any problems

回答4:

I followed the steps in the answer (cleaned and then made sure it's the id) and noticed that going to the source of my EditText R.id brings me to the EditText. Thought this is definitely not a IDE cache problem.

What I did do is to change the LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

to

android:layout_width="match_parent"

android:layout_height="match_parent"

For some reason it fixed the issue (I had this whole layout wrapped in something else that I just recently removed).

来源:https://stackoverflow.com/questions/17246695/android-widget-button-cannot-be-cast-to-android-widget-edittext

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值