0

I'm trying to pull Context in to a class using Dagger, here's what I have and the errors that ensue:

@Module(injects = { MyApp.class, TransportModule.class }, library = true, includes = { TransportModule.class })
public class AppModule {

    private final MyApp remoteApp;

    public AppModule(MyApp remoteApp) {
        this.remoteApp = remoteApp;
    }

    @Provides
    @Singleton
    Context provideApplicationContext() {
        return remoteApp;
    }

}

Application Class:

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();

        objectGraph = ObjectGraph.create(getModules().toArray());
        objectGraph.inject(this);

        mContext = getApplicationContext();
        private List<Object> getModules() {
        return Arrays.<Object>asList(new AppModule(this));
    }

    public ObjectGraph createScopedGraph(Object... modules) {
        return objectGraph.plus(modules);
    }

    public static Context getContext() {
        return mContext;
    }

    public static LoQooApp getInstance() {
        return instance;
    }

}

DeviceInfo.java:

public class DeviceInfo {
    static LoQooApp baseApp;
    @Inject
    static Context mContext;

    public DeviceInfo() {

    }

    public static boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(mContext);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                Log.v(TAG, Integer.toString(resultCode));
            } else {
                Log.i(TAG + "NOPE", "This device is not supported.");
            }
            return false;
        }
        return true;
    }

}

LogCat Error:

     Caused by: java.lang.NullPointerException: Attempt to invoke 
     virtual method 'android.content.pm.PackageManager 
     android.content.Context.getPackageManager()' on a null object   reference at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvai lable(Unknown Source)


     Caused by: java.lang.NullPointerException: Attempt to invoke 
     virtual method 'android.content.pm.PackageManager 
     android.content.Context.getPackageManager()' on a null object   
     reference
     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvai lable(Unknown Source)

There are whole bunch of methods in DeviceInfo that need context they all fail. How do I bring context into that Class via Dagger or even without Dagger?

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
sirvon
  • 2,547
  • 1
  • 31
  • 55
  • You don't want to keep a static reference to the `Context`. Either provide it as a parameter in your methods, or create a `DeviceInfo` instance which takes the `Context` in its constructor. Then you can inject the `DeviceInfo` instance into your classes using it. – nhaarman Jan 27 '15 at 15:55
  • Inject DeviceInfo in where? Other classes that use its methods like a BaseActivity? – sirvon Jan 27 '15 at 16:22

1 Answers1

1

Static injections can be done (How to inject into static classes using Dagger?), but this should be the exception. You should rather make both the method and the field not static.

So, DeviceInfo looks like

@Inject public DeviceInfo(Context context) {
    mContext = context;
}
public boolean checkPlayServices() { //not static

Then, DeviceInfo is injected

public class MyApp {
    @Inject DeviceInfo deviceInfo;

which is set by objectGraph.inject(this); in onCreate.

If you need DeviceInfo in activites, you also call inject in onCreate

MyApp app = (MyApp) getApplication();
app.getObjectGraph().inject(this);

You also need to add the activity to the injects part of the AppModule.

Community
  • 1
  • 1
  • can you give any example code because Dagger/DI is already a maze to me. I need to "see" what you are describing. ty – sirvon Jan 27 '15 at 16:24