Android.widget.Button cannot be added to android.widget.EditText

Developing my first application for an Android calculator, I was able to update the TextView in a new action, passing the response using intent, but this requires the user to hit Back to perform another calculation. I am trying to make the doCalculation button update a simple TextView in MainActivity and get an error:

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

Here is 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 is the code that successfully launched the 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, 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!" /> 

Thanks for the help, Michael

+6
source share
4 answers

Either R.id.number1 or R.id.number2 seems to be Button. Check your XML and make sure it is an EditText.

Edit: the original answer did not work, but cleaning up the project solved the problem.

+13
source

I had this problem. It seems that the xml layout file is not compiled properly. Rather, it is not included in the list of compiled files.

0
source

I had the same situation, but I found that there are two text images with the same identifiers in different actions, so I changed one of them and the program ran clearly, so check the identifiers of all your elements and buttons and change the sallere even if they were in other actions, and I think this will work without any problems.

0
source

I followed the steps in the answer (cleared and then made sure it was id) and noticed that going to the source of my EditText R.id leads me to EditText . Thought this was definitely not an IDE cache issue.

What I did was change 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, he fixed the problem (I had this whole layout wrapped in something else that I recently deleted).

0
source

Source: https://habr.com/ru/post/947886/


All Articles