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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:34:33+00:00 2026-05-28T02:34:33+00:00

I have an AsyncTask which calls my LocationHandler class method getLocation() which runs a

  • 0

I have an AsyncTask which calls my LocationHandler class method getLocation() which runs a Thread as well. I’m getting the following error:

Can't create handler inside thread that has not called Looper.prepare()

Some answers have included calling Looper related methods but I’d rather not do this as it’s bad practise

Main Activity calls the AsyncTask:

public class Main extends Activity {        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        StartProcess sProcess = new StartProcess();
        sProcess.execute(this);
    }
}

AsyncTask:

public class StartProcess extends AsyncTask<Main, Void, Void>
{
    @Override
    protected Void doInBackground(Main... params) {
        LocationHandler lh = new LocationHandler();
        try {
            lh.getLocation(null, params[0]);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}

LocationHandler, seems to crash when it calls requestLocationUpdates():

public class LocationHandler {
    LocationManager mlocManager;
    MyLocationListener mlocListener;
    Location location;

    public synchronized void getLocation(final View view, final Main main) throws InterruptedException
    {   
        mlocManager = (LocationManager)main.getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
            Looper.prepare();
        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5000, 1, mlocListener);
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 1, mlocListener);


        Thread uiThread = new HandlerThread("UIHandler"){
            public synchronized void run(){

                //stuff
                }

            }

        };
        uiThread.start();

    } 

LocationListener:

public class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location loc)
    {

        location = new Location(loc);

    }
  • 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-05-28T02:34:34+00:00Added an answer on May 28, 2026 at 2:34 am

    I used @nininho suggestion but I called Looper.prepare() just before I called requestLocationUpdates() in my LocationHandler class. I also had to call Looper.loop() just after I called the Thread otherwise the Thread wouldn’t even run.

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

Sidebar

Related Questions

I have a service, that requests another class, which launches an AsyncTask Service->Weather Class->Execute
I have a common class say for eg Class A which extends AsyncTask and
I have a class which extends AsyncTask, which is intended to serve as a
I have an AsyncTask which launches a DatabaseHelper class from DoinBackground which copies an
I have the following method which uses implicit scheduling: private async Task FooAsync() {
I have generic async task class which fetches response from server . And i
I have created an asynchronous task like this: private class LongOperationcheckall extends AsyncTask<String, String,
In my Activity I use a class which extends from AsyncTask and a parameter
I have an activity object, myAct. myAct creates and calls my httpGetter object which
I currently have a UI that uses an AsyncTask class to make a web

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.