Question

I have a problem with my app. My application is actually a service. I have Main extends Service, it has private Looper looper variable to get HandlerThread looper. In onCreate function I initialize location manager, location listener and HandlerThread (which sets its looper to looper variable), then I try to use requestLocationUpdates passing the looper variable as looper. I gets an error

09-22 17:30:24.069: E/AndroidRuntime(1414): Caused by: java.lang.IllegalArgumentException: looper==null

Should I do anything else to this HandlerThread? Maybe start it?:>

I don't paste any code since it's quite long and I don't know the relevant part which would be appropriate to solve the problem. Therefore I'd love to pass any code You may need (HandlerThread? Anything else?)

Thanks for any help.

** EDIT **

Alright, onCreate function:

public void onCreate() {
    super.onCreate();
    Log.d("Service", "Service onCreate starts");
    running = true;
    lt = new LooperThread("GPSIntentLT");
    serviceLocationM = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    serviceLocationL = new MyLocationListener();

    requestUpdates(serviceLocationL);
    Log.d("Service", "Service onCreate ends");
}

requestUpdates function (called above, there the error appears):

private void requestUpdates(LocationListener listener)
{
    Log.d("Service", "requestUpdates starts");
    serviceLocationM.removeUpdates(listener);
    flag = displayGpsStatus();
    switch(flag)
    {
    case 0:
        Log.d("Service", "No Location Provider");
        break;
    case 1:
        Log.d("Service", "Network Provider");
        serviceLocationM.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 25, listener, theLooper);
        break;
    case 2:
        Log.d("Service", "GPS Provider");
        serviceLocationM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 25, listener, theLooper);
        break;
    }
    Log.d("Service", "requestUpdates ends");
}

And HandlerThread:

private class LooperThread extends HandlerThread{

    public LooperThread(String name) {
        super(name);
        Log.d("Service", "LooperThread constructor starts");
        theLooper = getLooper();
        Log.d("Service", "LooperThread constructor ends");
    }

    @Override
    public void run() {
        super.run();
        Log.d("Service", "LooperThread run called");
    }

}

And at the end, logcat for this app:

09-22 18:21:47.997: D/Service(386): Service onCreate starts
09-22 18:21:47.997: D/Service(386): LooperThread constructor starts
09-22 18:21:48.007: D/Service(386): LooperThread constructor ends

So it does go down on requestLocationUpdates function, it happens on 2.2 emulator, on 2.3.3 it crashes the whole emulator by killing its processes (?).

Was it helpful?

Solution

From : http://developer.android.com/reference/android/os/HandlerThread.html#getLooper%28%29

This method returns the Looper associated with this thread. If this thread not been started or for any reason is isAlive() returns false, this method will return null. If this thread has been started, this method will block until the looper has been initialized.

Since you call this method in the constructor of the LooperThread, then of course you have not started it yet (by calling start()). The method returns null, which in itself is valid, and the constructor completes, but then you pass null later on in requestUpdates(), which causes the crash.

You will need to start the thread and then acquire a reference to the looper. Make sure it is ready before trying to use it in another method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top