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 8337511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:19:33+00:00 2026-06-09T04:19:33+00:00

I have a MapActivity that has code similar to this in onCreate(): if(…) {

  • 0

I have a MapActivity that has code similar to this in onCreate():

if(...) {
    ...
    new FirstAsyncTask(id, this).execute((Object[]) null);
    ...
} else {
    ...
    myLocationOverlay.runOnFirstFix(new Runnable() {
        new SecondAsyncTask(lat, lon, ActivityName.this).execute((Object[]) null);
    }
    ...
}

The FirstAsyncTask and SecondAsyncTask both do different things, but both of them show a ProgressDialog like so:

public FirstAsyncTask(long id, Context context) {
    progressDialog = new ProgressDialog(context);
}

protected void onPreExecute() {
    ...
    progressDialog.show();
}

protected String doInBackground(Object... params) {
    ...
    progressDialog.dismiss();
    ...
}

This is working with FirstAsyncTask, but no matter what I change in the call to SecondAsyncTask it always fails with this error: Can’t create handler inside thread that has not called Looper.prepare(). I have tried setting the context parameter to “this”, “ActivityName.this”, getApplicationContext(), and getBaseContext().

I’m still pretty new to Android so this idea of a “context” is confusing me. I’m even more confused that FirstAsyncTask works but SecondAsyncTask doesn’t. I’ve seen this error mentioned a lot in other questions but none of the answers seem to work. Any ideas?

EDIT: The exception is being thrown when the ProgressDialog is being initialized in the SecondAsyncTask’s constructor.

  • 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-09T04:19:34+00:00Added an answer on June 9, 2026 at 4:19 am

    The problem is here

    myLocationOverlay.runOnFirstFix(new Runnable() {
        new SecondAsyncTask(lat, lon, ActivityName.this).execute((Object[]) null);
    }
    

    SecondAsyncTask is being constructed on a newly spawned thread. not the UI thread.

    Create the one progressDialog in the activity. And then access the Activity’s progressDialog from the Asynctasks.

    public class MyActivity extends Activity {
        ProgressDialog mProgressDialog;
    
        onCreate(...){
            mProgressDialog = new ProgressDialog();
        }
    
        private class MyAsyncTask extends AsyncTask<...> {
            ...
            onPreExecute(...){
                mProgressDialog.show();
            }
    
            onPostExecute(...){
                mProgressDialog.dismiss();
            }
        }
    
        private class MyAsyncTask2 extends AsyncTask<...> {
            ...
            onPreExecute(...){
                mProgressDialog.show();
            }
    
            onPostExecute(...){
                mProgressDialog.dismiss();
            }
        }
    

    Do not attempt to perform progressDialog.dismiss() from the doInBackground, instead put it in postExecute which is running in the UI thread.

    You’ll want to set up flags once all this is working so that you only dismiss the progressdialog once both tasks are complete.

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

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
I have written one MapActivity class that is able to display a set of
I know this has been asked ALOT on here, but I have been scouring
I have an activity MyActivity that extends from MapActivity. In the .xml file containing
I am working on an app that has a homescreen. This homescreen should behave
I have a fairly basic app that only has two Activities, the splash screen
I am creating a home automation app that has allows plugin views. I have
I have unit tests to run my code as follows: MyActivity myActivity = new
I have on Service that must communicate with an Activity (MyActivity) through a BradcastReceiver.
I have registered ProximityAlert in MapActivity. No problem with catching alerts in BroadcastReceiver and

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.