Search code examples
androidandroid-servicemessage

NullPointerException when instantiating Messenger in Bound Service


Hi and thanks for the support.

I have a Service which is bound to my MainActivity.

When i'm trying to instantiate a new Messenger form inside the Service to communicate with the activity.

Messenger mService = new Messenger(mBinder);

Exactly when the execution gets to this point I get NullPointerException.

Here is the code of the Service:

public class LocalService extends Service {
    public int i;
    public ArrayList<String> al;
     // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    Messenger mService = new Messenger(mBinder);
    public Task t;

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    public void sayHello() {

        // Create and send a message to the service
        Message msg = Message.obtain(null, 1, 0, 0);
        try {
            mService.send(msg);
        } catch (RemoteException e) {
             e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("", "sono nel service");
        t= new Task();
        t.execute();
        return mBinder;
    }   

Here is my LogCat:

12-22 13:17:03.382: E/AndroidRuntime(1625): FATAL EXCEPTION: main
12-22 13:17:03.382: E/AndroidRuntime(1625): java.lang.RuntimeException: Unable to instantiate service com.example.quotes.LocalService: java.lang.NullPointerException
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2347)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.access$1600(ActivityThread.java:130)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Looper.loop(Looper.java:137)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.reflect.Method.invoke(Method.java:511)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at dalvik.system.NativeStart.main(Native Method)
12-22 13:17:03.382: E/AndroidRuntime(1625): Caused by: java.lang.NullPointerException
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Binder.queryLocalInterface(Binder.java:230)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.IMessenger$Stub.asInterface(IMessenger.java:27)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.os.Messenger.<init>(Messenger.java:139)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at com.example.quotes.LocalService.<init>(LocalService.java:21)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.Class.newInstanceImpl(Native Method)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at java.lang.Class.newInstance(Class.java:1319)
12-22 13:17:03.382: E/AndroidRuntime(1625):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2344)
12-22 13:17:03.382: E/AndroidRuntime(1625):     ... 10 more

Solution

  • Problem solved, I post this in case it might be of interest of somebody:

    A Messenger is Parcelable, and so can be put into an Intent extra. The activity calling bindService() attaches a Messenger as an extra on the Intent. The service obtains that Messenger from the Intent. When the service needs to send the message to the activity, the service send the message on the Messenger. The Handler on the main activity receives the message via handleMessage.

    PROBLEM SOLVED.