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);
}
}