0

I want change TextView content to the result of calculation in thread, but crashing when execution. Here's my code.

new Thread(new Runnable() {
        public void run() {
            while (i < 5) {
               i++;
            }
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    TextView txv = (TextView) getView().findViewById(R.id.txvone);
                    Log.d("123","i = "+ i);
                    txv.setText(i);//CRASH!!!
                }
            });
        }
}).start();

enter image description here

YH_WU
  • 73
  • 11

2 Answers2

2

You need to pass a String type to the setText() method. When you pass an integer type, it performs a lookup to the R(see : R) file for a string resource with the specified ID. Since the ID does not match any item in your strings.xml file, the exception thrown is ResourceNotFoundException.

Like Sree said, try the code below, it is guaranteed to work.

txv.setText(String.valueOf(i)));
Community
  • 1
  • 1
Clinkz
  • 716
  • 5
  • 18
0

In the documentation it says setText(int resid).

So, the int valu, you pass for this method should be a resource id (one from the R.string static member). It's a bit annoying, but I think, Android Studio warns you about the @ResourceId annotation on the prameter.

If you'd like to set the text to 5, you should do the String.valueOf(5) method call, as suggested in the comments.

Nagy Vilmos
  • 1,878
  • 22
  • 46