Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:59:10+00:00 2026-06-18T12:59:10+00:00

I have a handler used to display images in a specified interval loop, on

  • 0

I have a handler used to display images in a specified interval loop, on reaching the last image, it goes back to the first image which is the correct. However, i’m having problems with it as it’s causing some devices to crash and makes the CPU usage go up significantly, i’m just wondering what is wrong with the code?

I instantiate it like the following at the top of the fragment:

final public static Handler handler = new Handler();
boolean isRunning = false;

Then in the onPostExecute part of an AsyncTask, I have this code:

 @Override
         protected void onPostExecute(Void v) {

            if(!isRunning) {
             Runnable runnable = new Runnable() {

                @Override
                public void run() {



                    anImageView.setVisibility(View.VISIBLE);
                    isRunning = true;
                    counter++;

                    //imageDownloader.download(data.get(i).getImageURL(), anmageView);
                    if(TabsViewPagerFragmentActivity.theImages !=null && TabsViewPagerFragmentActivity.theImages.size() > 0){
                    Bitmap anImage = TabsViewPagerFragmentActivity.theImages.get(i);
                    anImageView.setImageBitmap(anImage);
                    }

                    i++;
                    if(i>TabsViewPagerFragmentActivity.theImages.size()-1)
                    {
                    i=0;    
                    }



                    handler.postDelayed(this, 1500); 
                }




             };
             handler.postDelayed(runnable, 0);




          }  

         }

The above AsyncTask is called within the onCreate() method.
Secondly, I have a refresh button which re-downloads these images in order to get the latest ones as they change periodically. Therefore I have an onClick() event attached to the refresh button. This also works fine but here is the code which is called:

@Override
     protected void onPostExecute(Void v) {

         for(int i=0;i<data.size()-1;i++) {

            Bitmap anImage = getBitmapFromURL(data.get(i).getImageURL());
            theImagesRefreshed.add(anImage);

         }



         if(!isRunning) {
             Runnable runnable = new Runnable() {

                @Override
                public void run() {



                    anImageView.setVisibility(View.VISIBLE);
                    isRunning = true;
                    counter++;

                    //imageDownloader.download(data.get(i).getImageURL(), anImageView);
                    if(theImagesRefreshed !=null && theImagesRefreshed.size() > 0){
                    Bitmap anImage = theImagesRefreshed.get(i);
                    anImageView.setImageBitmap(anImage);
                    }

                    i++;
                    if(i>theImagesRefreshed.size()-1)
                    {
                    i=0;    
                    }



                    handler.postDelayed(this, 1500); 
                }




             };
             handler.postDelayed(runnable, 0);


         }


     }

I think that the handler is not setup right and is causing the performance issues. Can anyone see anything wrong with this code?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:59:11+00:00Added an answer on June 18, 2026 at 12:59 pm

    You need to call Looper.prepare() while using handlers in threads .So write Looper.prepare() after you are creating instance of Runnable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a handler and a runnable, Handler has a loop that runs from
We have a requirment which requires to have an Handler that is extended from
I have a message handler, which consumes from a JMS queue and that sends
I have a problem with an event handler used to resort a table with
I have used multiple activities to handle mutiple views in android . I found
I used BeautifulSoup to handle XML files that I have collected through a REST
I have a handler and I have to send responses to different activities for
I have a Handler class that looks like this: class Handler{ public $group; public
In my APP. Need to use Lazy Loading, so I have to handler the
I have an IPN handler, but it seems it isn't getting any IPN's from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.