简体   繁体   中英

Application Context Using Dagger?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM