Issue
I am trying to create an auto image slider using a view pager following some tutorials. I got everything working, but then I see
Choreographer: Skipped 1 frames! The application may be doing too much work on its main thread.
and googled the error.`
You see, in my code, I have a timer and a handler that just delay the code for 3 seconds and then slides the view pager to the next image. I confirmed with a friend and after hours of searching, there was nothing else that was doing too much work on the main thread.
So searching on stack overflow about the problem, I see that a lot of developers suggested using AsyncTask to do some stuff in background and then update, which might actually be a perfect solution here. But then I realised that I know nothing about AsyncTask. I went to the android developers reference and saw some tutorials, but I was unable to find something that'll fit into my solution.
I saw a lot of tutorials about image downloading, but they are by far, not what I'm concerned with. I am only concerned with controlling the view pager to move to the next slide.
Here is the part where I control my view pager to slide:
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 3000, 3000);
How do I incorporate this code into an AsyncTask?
For reference, I used this tutorial.
Solution
Runnable is a seprate worker thread, it has no interaction with main thread. When you are handling ui elements, use runOnUiThread. example.
runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
});
or
runOnUiThread(){
mPager.setCurrentItem(currentPage++, true);
}
Answered By - ankur uniyal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.