在Dagger中注入返回null

时间:2016-03-28 10:23:30

标签: java android dagger

已被封锁了好几天。

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

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

}

我花了很多时间思考这个问题,我看过无数的例子和博客网站。我会更聪明地爱一个人,然后帮忙。

感谢阅读。

1 个答案:

答案 0 :(得分:2)

字段注入仅适用于直接调用对象图。要获取提供者方法的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 {