Context.getApplicationContext () returns null in the service

I create an IabHelper object in the service, but sometimes it throws a NullPointerException, which I found is related to context.getApplicationContext(), which the nullint constructor of IabHelper sometimes returns .

In other words, context.getApplicationContext()sometimes returns null, but I can’t understand why.

The IabHelper object is described here , slightly modified.

Here's what I do: in the onCreate () method of the service, create an IabHelper object:

public class MyService extends Service {

    IabHelper helper;
    ...

    public void onCreate() {
      super.onCreate();
      ...
      helper = new IabHelper(this);
      ...
    }
}

This thisshould not be null. The IabHelper object looks something like this:

public class IabHelper {
  Context context;

  public IabHelper(Context ctx) {
    context = ctx.getApplicationContext();
    ...
  }
  ...
}

, , context.getPackageName() , NullPointerException. 7 50 ( ).

, , - , , .

? .

+4

Source: https://habr.com/ru/post/1623961/


All Articles