0

From my mobile I am starting a foreground service in my android watch. I am able to do that correctly. But when I try to stop the service as well as the activity in watch , I am getting an error message( watch app is crashing). I am using message listener service to receive the message from mobile.

I also have a stop button in watch app which is running perfectly(if I try to stop the service from watch itself) and it has the same lines of code. I am not sure where I am wrong when I receive message from mobile app to stop the foreground service.

@Override public void onMessageReceived(MessageEvent messageEvent) {

    Log.i(INFOTAG, "Stop msg received");

    if(messageEvent.getPath().equals(STOP_MESSAGE_PATH_2)){

        started = false;

        Intent stopIntent = new Intent(
                MainActivity.this, ForegroundService.class);
        stopService(stopIntent);

        this.finish();

    }
}

Below is the error log I am getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.(ComponentName.java:77)
at android.content.Intent.(Intent.java:4160)
at com.magi.magidemo.MainActivity.onMessageReceived(MainActivity.java:464)
at com.google.android.gms.wearable.internal.zzcf$5.zza(Unknown Source)
at com.google.android.gms.wearable.internal.zzcf$5.zzs(Unknown Source)
at com.google.android.gms.internal.zzmn.zzb(Unknown Source)
at com.google.android.gms.internal.zzmn$zza.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 Answer 1

0

You need to get the Activity's instance from the override method.

Like this:

public MainActivity activity;

// in the override method

this.activity = activity;

Then use it:

Intent stopIntent = new Intent(
            activity, ForegroundService.class);

Have a look: android.content.Context.getPackageName()' on a null object reference

3
  • Hi, how to use the Activity's instance in onMessageReceived override method? Please advise.
    – TheRedOne
    Jan 26, 2016 at 8:32
  • Add the first line in your Activity, i think the problem is coming from **this**.finish(); and Intent stopIntent = new Intent( activity, seems to be good. it should work with my answer. take a look at this answer: stackoverflow.com/a/30498143/4409113 Jan 26, 2016 at 8:34
  • Hi, the activity is actually closing. Only problem is that the foreground service is running with error message. I tried it. Still getting error.
    – TheRedOne
    Jan 26, 2016 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.