0

I have a problem when trying to start a messenger between my activity and a service. On my activity, I have this:

Messenger bluetoothServiceMessenger;
private ServiceConnection bluetoothServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            bluetoothServiceMessenger = new Messenger(service);
... }

As you can see, i'm trying to create the messenger using the IBinder. On my service, the method onBind() is this one:

@Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "BluetoothService has been started.");
        return bluetoothServiceBinder;
    }
public class BluetoothServiceBinder extends Binder {
        public BluetoothService getService() {
            return BluetoothService.this;
        }
    }

So, the problem is I'm getting the following NullPointerException on the onServiceConnected() method:

04-16 20:15:32.365 2117-2117/com.alejandro_castilla.heartratetest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.alejandro_castilla.heartratetest, PID: 2117
                                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                        at android.os.Binder.queryLocalInterface(Binder.java:254)
                                                                                        at android.os.IMessenger$Stub.asInterface(IMessenger.java:27)
                                                                                        at android.os.Messenger.<init>(Messenger.java:146)
                                                                                        at com.alejandro_castilla.heartratetest.MainActivity$1.onServiceConnected(MainActivity.java:129)
                                                                                        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1223)
                                                                                        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1240)
                                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I've been trying some things but nothing works. I can confirm that the service works correctly if I don't this, so I think it isn't a problem with binding. Any help would be appreciated.

Thanks in advance.

  • 1
    Can you provide the whole `onServiceConnected()` method? – Pritam Banerjee Apr 16 '16 at 18:29
  • I don't think you can create a `Messenger` that way. The docs for that `Messenger` constructor say "Create a Messenger from a raw IBinder, **which had previously been retrieved with getBinder()**" (emphasis added). – CommonsWare Apr 16 '16 at 18:33

0 Answers0