java - 应用程序上下文使用 Dagger?

标签 java android singleton dagger

我正在尝试使用 Dagger 将 Context 拉入类中,这是我所拥有的以及随之而来的错误:

@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;
    }

}

应用程序类:

    @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 错误:

     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)

DeviceInfo 中有一大堆需要上下文的方法,但它们都失败了。 如何通过 Dagger 甚至不使用 Dagger 将上下文引入该类?

最佳答案

可以进行静态注入(inject)( How to inject into static classes using Dagger? ),但这应该是异常(exception)。您应该使方法和字段都不是静态的。

所以,DeviceInfo 看起来像

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

然后,注入(inject)DeviceInfo

public class MyApp {
    @Inject DeviceInfo deviceInfo;

由objectGraph.inject(this)设置;在 onCreate 中。

如果在activites中需要DeviceInfo,也可以在onCreate中调用inject

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

您还需要将 Activity 添加到 AppModule 的注入(inject)部分。

关于java - 应用程序上下文使用 Dagger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164848/

相关文章:

java - 如何在 Java 中向 hibernate 线程发出信号?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

jakarta-ee - 针对特定客户端-服务器系统的建议

android - 未找到 Gradle DSL 方法 : 'applicationId()'

ios - 我应该将 appDelegate 用于所有单例样式的应用程序级代码吗?

C++:单例?如何将参数传递给构造函数?

java - Java 类中的静态 main

javascript - 在输入字段模糊时隐藏 Nativescript 中的 Android 键盘

java - 使用多线程设置线程优先级没有效果

ios - 替代使用 Singleton 或 AppDelegate 进行全局数据存储