0

i have images array and i want to change background image every 5 second with rundom. i wrote some code but i have android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views,Exeption. what is this problem solution.

public class StradaContact extends Fragment {

private int[] image_rundow = {

R.drawable.slideone, R.drawable.slidetwo, R.drawable.slidetree

};

private ImageView mapimg;
Reminder claass;

public StradaContact() {

}

public static StradaContact newInstance() {
    return new StradaContact();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.strada_contact, container,
            false);

    claass = new Reminder(5);

    return rootView;
}

public class Reminder {

    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds * 1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            while (true) {
                Random rand = new Random();

                int index = rand.nextInt(image_rundow.length);

                mapimg.setBackgroundResource(image_rundow[index]);

                timer.cancel();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            // Terminate the timer thread
        }
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

}

BekaKK
  • 2,173
  • 6
  • 42
  • 80
  • As the exception says, only the thread that created a view can change its properties. Look at AysncTask or View.PostDelayed – CurlyPaul Jul 23 '14 at 16:33
  • possible duplicate of [Threading UI updates in Android](http://stackoverflow.com/questions/3745405/threading-ui-updates-in-android) – CurlyPaul Jul 23 '14 at 16:35

1 Answers1

0

I have improved your class: 1. Timer has third parametr - interval. 2. UI code for UI thread (it is about mapimg.setBackgroundResource(...) ) How cancel task you know. You can do it without help.

public class Reminder {
        Timer timer;
        public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds * 1000,5000);
        }

        class RemindTask extends TimerTask {
            public void run() {
              getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Random rand = new Random();
                            int index = rand.nextInt(image_rundow.length);
                            mapimg.setBackgroundResource(image_rundow[index]);
                        }
                    });
              }
        }
}

P.S I don't check code. Maybe it has some mistakes

dreambit.io dreambitio
  • 1,892
  • 1
  • 15
  • 24