Skip to content Skip to sidebar Skip to footer

Only The Original Thread That Created A View Hierarchy Can Touch Its Views. On Android

I'm only a beginner so please forgive me for asking possibly a stupid question I don't understand the meaning of Only the original thread that created a view hierarchy can touch i

Solution 1:

Change your startingUp() to this.

privatevoidstartingUp() {
    Thread timer = newThread() { //new thread         publicvoidrun() {
            Boolean b = true;
            try {
                do {
                    counter++;
                    title();
                    sleep(1000);

                    runOnUiThread(newRunnable() {  
                    @Overridepublicvoidrun() {
                        // TODO Auto-generated method stub

                        title.clearComposingText();
                    }
                });


                }
                while (b == true);
            } catch (IntruptedException e) {
                e.printStackTrace();
            }
            finally {
            }
        };
    };
    timer.start();
}

You can't modify views from non-UI thread.

Solution 2:

This exception is not coming due to title.clearComposingText(). even this line is not useful ,we can remove this line. This exception is coming in to title() function because a non UI-Thread is trying to modify view. so we need to call this function in a UI Thread or Handler.

privatevoidstartingUp() {
        Thread timer = newThread() { //new thread         publicvoidrun() {
                boolean b = true;
                try {
                    do {
                        counter++;

                        sleep(1000);
                        runOnUiThread(newRunnable() {

                            @Overridepublicvoidrun() {
                                  title();
                                //title.clearComposingText();//not useful

                            }
                        });


                    }
                    while (b == true);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                }
            };
        };
        timer.start();
    }

Solution 3:

You can't modify the text with title.clearComposingText(); inside the thread because you can only modify views from the UI thread. Use a handler instead and let him change the text.

Solution 4:

You should not update textView from thread other than UI thread.You can use asynctask for this.Can refer this

Solution 5:

As other people already stated, you cannot modify the UI from a background thread.

You can either use AsyncTask, or use the Activity.runOnUiThread() method

Post a Comment for "Only The Original Thread That Created A View Hierarchy Can Touch Its Views. On Android"