S
S
Sland Show2016-09-10 00:06:00
Java
Sland Show, 2016-09-10 00:06:00

How to fix error when using multithreading?

Good day.
I am writing a stopwatch for Android .
Problem when using multithreading:

09-09 20:43:47.488 3646-3685/com.example.admin.firstappdemo E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4014920
09-09 20:44:41.774 3942-4013/com.example.admin.firstappdemo E/AndroidRuntime: FATAL EXCEPTION: Thread-159
                                                                              Process: com.example.admin.firstappdemo, PID: 3942
                                                                              android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                                  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
                                                                                  at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:942)
                                                                                  at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081)
                                                                                  at android.view.View.invalidateInternal(View.java:12713)
                                                                                  at android.view.View.invalidate(View.java:12677)
                                                                                  at android.view.View.invalidate(View.java:12661)
                                                                                  at android.widget.TextView.checkForRelayout(TextView.java:7151)
                                                                                  at android.widget.TextView.setText(TextView.java:4342)
                                                                                  at android.widget.TextView.setText(TextView.java:4199)
                                                                                  at android.widget.TextView.setText(TextView.java:4174)
                                                                                  at com.example.admin.firstappdemo.MainActivity.updateScreen(MainActivity.java:111)
                                                                                  at com.example.admin.firstappdemo.MainActivity.access$300(MainActivity.java:14)
                                                                                  at com.example.admin.firstappdemo.MainActivity$1.run(MainActivity.java:85)
                                                                                  at java.lang.Thread.run(Thread.java:818)
09-09 20:44:41.889 3942-3959/com.example.admin.firstappdemo E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4014ca0

For some reason, the problem arises precisely when I try to update the display in the second thread (which calculates the time). And in general, the error occurs when the setText () method is called on the TextView in the second thread.
This is where the link from the stack trace takes me:
private void updateScreen() {
        String min = "" + minutes;
        String sec = "" + seconds;
        String delim = " : ";
        display = min + delim + sec;  // screen looks like -> minutes : seconds
        screen.setText(display);
    }

private void startProcess() {

        //start calculate time
       new Thread(new Runnable() {
            @Override
            public void run() {
                while (start && !stop) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    calculateTime();
                    updateScreen(); //здесь почему-то показывает ошибку
                }

            }
        }).start();


    }


    private void calculateTime() {

        seconds++;
        if (seconds == 60) {
            minutes++;
            seconds++;
        }

    }

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question