-1

I have created an app composed of one single activity which include a single TextView. Here is the XML of my activity :

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="zeratops.myApp.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#000000"
        android:layout_alignParentTop="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:text=""
            android:textColor="#FF0000"
            android:id="@+id/text_1"
            android:paddingTop="200dp"
            android:layout_gravity="center" />
    </FrameLayout>
</RelativeLayout>

My java code is to set the text of this textView every second. Here is the code I use to perform this task :

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView text = (TextView) findViewById(R.id.text_1);
        Timer timer = new Timer();
        TimerTask timertask = new TimerTask() {
            @Override
            public void run() {
                text.setText("test");
            }
        };

        timer.schedule(timertask, 0, 1000);
    }
}

The issue

I am facing the following issue when I launch load my app and launch it on my LG G2 :

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I identified the lines :

timer.schedule(timertask, 0, 1000);

causing the error since I do not have exceptions when I remove it. Am I mising some checking ?

Anwar
  • 4,162
  • 4
  • 41
  • 62
  • In JDK you would wrap `text.setText("test");` with `Platform.runLater(){}`, android should have something similar. – shinzou Sep 16 '16 at 22:52

2 Answers2

0

You need to perform

text.setText("test");

in UI thread. You can do so using Handlers.

final Handler myHandler = new Handler();
myHandler.post(myRunnable);



   final Runnable myRunnable = new Runnable() {
      public void run() {
        text.setText("test");
      }
   };
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
0

Your run() method will be called on a background thread, and it cannot manipulate the UI as a result.

A lighter-weight solution would be:

public class MainActivity extends AppCompatActivity {
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.text_1);

        onEverySecond.run();
    }

    private Runnable onEverySecond=new Runnable() {
        @Override
        public void run() {
            text.setText("test");
            text.postDelayed(this, 1000);
        }
    }
}

postDelayed() calls your run() method on the main application thread, after the designated delay period. Here, we use that to have the Runnable schedule itself to run again after 1000ms.

To stop the postDelayed() "loop", call removeCallbacks(onEverySecond) on your MainActivity.

The advantage of this over using runOnUiThread() inside the TimerTask is that it is less expensive, as you are not creating a separate thread (the Timer/TimerTask thread).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Perfect. Just to mention, I had to put the `final TextView text = (TextView) findViewById(R.id.text_1);` instanciation inside your `public void run() {` function in order to avoid scope errors. But without it, it saves the day pretty well ! – Anwar Sep 16 '16 at 23:03
  • @Zeratops: Or, make it a field -- I just updated my answer to show that. It saves you from making the `findViewById()` call unnecessarily every second. – CommonsWare Sep 16 '16 at 23:16
  • I checked it works correctly. Thank your for your update. – Anwar Sep 16 '16 at 23:21