0

Timer is a TextView and during run times throws error

[threadid=1: thread exiting with uncaught exception (group=0xa4b5c648)]

[FATAL EXCEPTION: main]

[android.content.res.Resources$NotFoundException: String resource ID #0x0]

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.level_twolayout);
    Thread t1 = new Thread() {
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            sleep(1000);
                        } catch (Exception e) {

                        }
   error-------->   Timer.setText(i);
                    }
                }
            };t1.start();
    }
rupesh
  • 2,865
  • 4
  • 24
  • 50
Veera Prasad
  • 61
  • 1
  • 8
  • http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – EpicPandaForce Sep 04 '14 at 08:58
  • A gentle advice and a precaution for all the future exception you might encounter ----> handle any/just UI operations in ui thread like this `runOnUiThread(new Runnable(){ Timer.setText(i); } );` – BlackBeard Sep 04 '14 at 10:24

2 Answers2

1

You have to set the text like this

Timer.setText(i + "");

This is because the setText(int resId) version will be invoked when you pass in an int value when you really wanted to invoke the setText(String text) version

The compiler/IDE does not give an error here. The int version looks for a corresponding String resource which doesn't exist and gives you the error.

Check this link for mode info TextView.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
1

change

Timer.setText(i);

to

Timer.setText(String.valueOf(i));

Here i is an Integer value. You cann't assign Integer value as TextView text you have to convert it to String first.

user3384985
  • 2,975
  • 6
  • 26
  • 42