-1

Today I am trying to make a calculator, but my clear button is throwing error. MainActivity.java https://gist.github.com/meanindra/b825505e300ecf2e0cf2c54f6e732a8e ,

activity_main.xml https://gist.github.com/meanindra/7cc87c0ee3a99653bd0b030c64b8d47d and Logcat https://gist.github.com/meanindra/a04cf93978d17117e8331db9fe5aaea6

I am unable to find what is the error. Please help me.

Anindra
  • 9
  • 4
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Nov 15 '21 at 16:41

1 Answers1

0

I think you passed an empty string with zero characters in this function updateText(""). There is no text inside the "" tag. So it will crash because you are trying to setSelection in the editText with character less than (cursorPosi + 1) in this line.

binding.display.setSelection(cursorPosi + 1);

To fix this error you can try calling the function with white space:

updateText(" ");

But I wonder that you will need to clear all character or detele the character at cursor of the EditText when you click on clear button