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?