Veera Prasad
Veera Prasad

Reputation: 61

Thread shows error

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

Upvotes: 0

Views: 51

Answers (2)

user3384985
user3384985

Reputation: 3017

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.

Upvotes: 1

midhunhk
midhunhk

Reputation: 5554

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.

Upvotes: 1

Related Questions