20

I am trying to run the for loop in every half second.The loop is changing something in the view every time it is called,but the changes are made by another class i.e Speedometer.

Thread thread=new Thread(){
        @Override
        public void run() {
            float i;
            try {

                for ( i = 0; i <= 100; i++) {
                            Speedometer1.onSpeedChanged(Speedometer1.getCurrentSpeed(i) + 8);
                            Speedometer2.onSpeedChanged(Speedometer2.getCurrentSpeed(i) + 8);
                            Speedometer3.onSpeedChanged(Speedometer3.getCurrentSpeed(i) + 8);
                            Speedometer4.onSpeedChanged(Speedometer4.getCurrentSpeed(i) + 8);
                            Speedometer5.onSpeedChanged(Speedometer5.getCurrentSpeed(i) + 8);
                            Speedometer6.onSpeedChanged(Speedometer6.getCurrentSpeed(i) + 8);


                    sleep(500);
                }
            }
            catch (InterruptedException e)
            {e.printStackTrace();}
        }
    };thread.start();
Ariel
  • 231
  • 1
  • 2
  • 5
  • 2
    refer [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) – Ravi Aug 03 '16 at 12:17

4 Answers4

24

That's because any view can be only modify in the main thread or ui Thread. Try running onSpeedChanged() using runOnUiThread(). Like this:

    Thread thread=new Thread(){
    @Override
    public void run() {
        float i;
        try {

            for ( i = 0; i <= 100; i++) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Speedometer1.onSpeedChanged(Speedometer1.getCurrentSpeed(i) + 8);
                        Speedometer2.onSpeedChanged(Speedometer2.getCurrentSpeed(i) + 8);
                        Speedometer3.onSpeedChanged(Speedometer3.getCurrentSpeed(i) + 8);
                        Speedometer4.onSpeedChanged(Speedometer4.getCurrentSpeed(i) + 8);
                        Speedometer5.onSpeedChanged(Speedometer5.getCurrentSpeed(i) + 8);
                        Speedometer6.onSpeedChanged(Speedometer6.getCurrentSpeed(i) + 8)

                    }

                });


                sleep(500);
            }
        }
        catch (InterruptedException e)
        {e.printStackTrace();}
    }
};thread.start();
Miguel Benitez
  • 2,322
  • 10
  • 22
13

You should update the UI Components in runOnUIThread.Sample code is

runOnUiThread(new Runnable() {
     @Override
     public void run() {

       //stuff that updates ui

    }
});
Mujammil Ahamed
  • 1,454
  • 4
  • 24
  • 50
10

You should update your UI on the UI thread.

For that, create a handler in your onCreate() method:

private Handler mHandler;

@Override
public void onCreate() {
    mHandler = new Handler();
    // ...
}

Then call the handler in your separate thread to execute it on the main thread:

mHandler.post(new Runnable() {
    @Override
    public void run() {
        // Update your UI
    }
});
xdevs23
  • 3,824
  • 3
  • 20
  • 33
0

You cannot modify View objects from outside the main thread. Even though your Speedometer class is making the changes, it is running in the secondary thread you have created.

You could create a Runnable and post with deejay to a Handler created by the main thread, or use other similar techniques to accomplish the same.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33