application stop working when i add picasso in timer to change url of image every 2 min
I want to get array of image url from web and put it in image view very 2min change the image i am using picasso and it worked for on url but when i put in timer the application stop
final String [] url = {"https://png.pngtree.com/thumb_back/fh260/back_pic/00/03/20/63561dc0bf71922.jpg",
"https://placeit-assets.s3-accelerate.amazonaws.com/landing-pages/make-a-twitch-banner2/Twitch-Banner-Blue-1024x324.png",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQgYdaf-JhDiFVeQjL6ZRskiF1CRADiJfgDKI3PKBfCMrnnPcHP"};
Timer adtimer = new Timer();
adtimer.schedule(new TimerTask() {
int count = 0 ;
@Override
public void run() {
ImageView Image_view = new ImageView( getActivity());
count++;
if(count >= url.length )
count = 0;
Picasso.get()
.load(String.format(url[count]))
.fit()
.into(Image_view);
}
} , 200 , 5000);
Timer's run
is not UI thread that's why you're getting an error. Put Picasso inside runOnUiThread
as below:
adtimer.schedule(new TimerTask() {
int count = 0 ;
@Override
public void run() {
ImageView Image_view = new ImageView( getActivity());
count++;
if(count >= url.length )
count = 0;
// Any view update should be made in UIThread
runOnUiThread(new Runnable() {
@Override
public void run() {
Picasso.get()
.load(String.format(url[count]))
.fit()
.into(Image_view);
}
});
}
} , 200 , 5000);