[Android Q&A, Help & Troubleshooting] lebossejames:
Hello,

I use AppWidgetProvider on Android, this bindService Foreground widget and then when it calls Messenger I get this error (works fine if it is Activity), mMessengerService is null in sendMess method:

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has
// been established, giving us the service object we can use
// to interact with the service. Because we have bound to a
// explicit service that we know is running in our own
// process, we can cast its IBinder to a concrete class and
// directly access it.

//mBoundService = ((MCBluetooth.LocalBinder)service).getService();
mMessengerService = new Messenger(service);
//mMessengerService=((MCBluetooth.LocalBinder)service).getMessenger();
Log.v(TAG,"onServiceConnected");

// Tell the user about this for our demo.
Toast.makeText(mContextWidget,
TAG+" service MCBluetoothForeground connected",
Toast.LENGTH_SHORT).show();
}

public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has
// been unexpectedly disconnected -- that is, its process
// crashed. Because it is running in our same process, we
// should never see this happen.
// mBoundService = null;
mMessengerService=null;
Toast.makeText(mContextWidget.getApplicationContext(),TAG+" service MCBluetoothForeground disconnected",Toast.LENGTH_SHORT).show();
}
};

void doBindServiceForeGround() {
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation
// that we know will be running in our own process (and thus
// won't be supporting component replacement by other
// applications).
Intent intent=new Intent(mContextWidget, MCBluetoothForeGround.class);

mContextWidget.getApplicationContext().bindService(intent,
mConnection,Context.BIND_AUTO_CREATE);
mIsBound = true;
}

void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContextWidget.getApplicationContext().unbindService(mConnection);
mIsBound = false;
}
}

private void sendMess(){

try {
Message message=new Message();
message.what=10;

if(mMessengerService==null){
Log.v(TAG,"mMessengerService is null");
}

mMessengerService.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}

}

2021-05-18 21:21:24.043 11598-11598/fr.jm.managercamera E/AndroidRuntime: FATAL EXCEPTION: main
Process: fr.jm.managercamera, PID: 11598
java.lang.RuntimeException: Unable to start receiver fr.jm.managercamera.MainActivityWidget: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Messenger.send(android.os.Message)' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3264)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:251)
at android.app.ActivityThread.main(ActivityThread.java:6572)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Messenger.send(android.os.Message)' on a null object reference
at fr.jm.managercamera.MainActivityWidget.sendCommand(MainActivityWidget.java:534)
at fr.jm.managercamera.MainActivityWidget.processVideo(MainActivityWidget.java:487)
at fr.jm.managercamera.MainActivityWidget.onReceive(MainActivityWidget.java:286)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3257)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1682)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:251)
at android.app.ActivityThread.main(ActivityThread.java:6572)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Why?

Thank you.