简体   繁体   中英

Inject in Dagger returns null

Been blocked on this for a number of days.

For some reason my the 'application' member field in AndroidModule.java is null after injection.

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

}

I've spend ages pondering over this, I've looked at countless examples and blog sites. Would love someone smarter then I to assist.

Thanks for reading.

Field injection works only with direct invocation of the object graph. To obtain the application instance for your provider method, you need to reference it as a parameter of the provider method.

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

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

Also, you never actually provided a MittoApplication , only an Application .

And it's worth noting that you are using Dagger1, so I believe you will need to change your AndroidModule to be complete=false :

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

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