在 Dagger 中注入 returns null

Inject in Dagger returns null

这几天被屏蔽了。

出于某种原因,我在 AndroidModule.java 中的 'application' 成员字段在注入后为空。

AndroidModule.java

@Module(
        library = true
)
public class AndroidModule {

    @Inject MittoApplication application;

    @Provides @Singleton
    SharedPreferences provideSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences( application );
    }

}

ApplicationModule.java

@Module(
        includes = { ApiModule.class, AndroidModule.class },
        library = true
)
public class ApplicationModule {

    private Application application;
    public ApplicationModule( Application application ) {
        this.application = application;
    }

    @Provides @Singleton
    Application providesApplication() {
        return application;
    }
}

BaseActivity.java

public class BaseActivity extends AppCompatActivity {

    private ObjectGraph objectGraph;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        objectGraph = MittoApplication.getApplication(this).buildScopedObjectGraph(getModules());
        objectGraph.inject(this);
    }

    @Override
    protected void onDestroy() {
        objectGraph = null;
        super.onDestroy();
    }

    protected List<Object> getModules(  ) {
        return Arrays.<Object>asList(
        );
    }
}

MittoApplication.java

public class MittoApplication extends Application {

    private static Context context;
    private ObjectGraph objectGraph;

    public void onCreate() {
        super.onCreate();
        MittoApplication.context = getApplicationContext();
        initializeObjectGraph();
    }

    public static MittoApplication getApplication( Context context ) {
        MittoApplication mittoApplication = (MittoApplication) context.getApplicationContext();
        return mittoApplication;
    }

    public ObjectGraph getObjectGraph() {
        return this.objectGraph;
    }

    public ObjectGraph buildObjectGraph( List<Object> modules ) {
        return ObjectGraph.create(modules.toArray() );
    }

    public ObjectGraph buildScopedObjectGraph( List<Object> modules ) {
        return objectGraph.plus(modules.toArray() );
    }

    private ObjectGraph buildInitialObjectGraph( List<Object> modules ) {
        return ObjectGraph.create( modules.toArray() );
    }

    private void initializeObjectGraph() {
        objectGraph = buildInitialObjectGraph( Arrays.<Object>asList( new ApplicationModule(this) ));
    }

    public static Context getContext() {
        return MittoApplication.context;
    }

}

我花了很长时间思考这个问题,我看过无数的例子和博客网站。会喜欢比我更聪明的人来帮忙。

感谢阅读。

字段注入仅适用于直接调用对象图。要为您的提供者方法获取 application 实例,您需要将其作为提供者方法的参数进行引用。

//@Inject MittoApplication application; //this won't work

@Provides @Singleton
SharedPreferences provideSharedPreferences(Application application) { //this will
    return PreferenceManager.getDefaultSharedPreferences( application );
}

此外,您实际上从未提供过 MittoApplication,仅提供了 Application

值得注意的是您使用的是 Dagger1,因此我相信您需要将 AndroidModule 更改为 complete=false:

@Module(
        library = true,
        complete = false
)
public class AndroidModule {