-1

I'm not sure why it happen and how to solve it. I have this method:

@Override
        public boolean onTouchEvent(MotionEvent event)
        {
            float eventX = event.getX();
            float eventY = event.getY();

            float lastdownx = 0;
            float lastdowny = 0;

            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    lastdownx = eventX;
                    lastdowny = eventY;

                    Thread t = new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            byte[] response = null;
                            if (connectedtoipsuccess == true)
                            {

                                if (is_start == true)
                                {
                                    response = Get(iptouse + "start");
                                    is_start = false;
                                } else
                                {
                                    response = Get(iptouse + "stop");
                                    is_start = true;
                                    servercheckCounter = 0;
                                }
                                if (response != null)
                                {
                                    String a = null;
                                    try
                                    {
                                        a = new String(response, "UTF-8");
                                        if (a.equals("Recording started"))
                                        {
                                          status1.setText("Recording");
                                        }
                                        if (a.equals("Recording stopped and preparing the file to be shared on youtube"))
                                        {
                                            status1.setText("Recording Stopped");
                                        }
                                        textforthespeacch = a;
                                        MainActivity.this.initTTS();
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                    }
                                    Logger.getLogger("MainActivity(inside thread)").info(a);
                                }
                            }
                        }
                    });
                    t.start();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    return false;
            }
            return true;
        }

The exception haapen after this line:

a = new String(response, "UTF-8");

Or this line:

status1.setText("Recording");

status1 is a TextView and in the onCreate i did:

status1 = (TextView) findViewById(R.id.textView3);

Make the program crash. The eexception message from the logcat:

09-19 10:22:18.458  19836-20124/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-78258
    Process: com.test.webservertest, PID: 19836
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6334)
            at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:890)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
            at android.view.View.requestLayout(View.java:17407)
            at android.widget.TextView.checkForRelayout(TextView.java:7054)
            at android.widget.TextView.setText(TextView.java:4186)
            at android.widget.TextView.setText(TextView.java:4044)
            at android.widget.TextView.setText(TextView.java:3999)
            at com.test.webservertest.MainActivity$6.run(MainActivity.java:633)
            at java.lang.Thread.run(Thread.java:818)
  • You're trying to modify a UI element from a non-UI thread. On Android, you can't do that. – Gergely Kőrössy Sep 19 '15 at 08:11
  • possible duplicate of [Android "Only the original thread that created a view hierarchy can touch its views."](http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) – Henry Sep 19 '15 at 08:15

1 Answers1

0

The error message states: "Only the original thread that created a view hierarchy can touch its views." Looking at your program, you actually try to set a text on some view in a thread that is not the main thread.

In situations like this you can use the runOnUiThread() method.

For example:

runOnUiThread(new Runnable() {
    status1.setText("Recording");
});
Henry
  • 42,982
  • 7
  • 68
  • 84