Android注入类返回null

时间:2017-04-16 06:22:05

标签: java android dependency-injection dagger-2 dagger

我正在使用JobQueue库,我可以成功地注入一些类,例如GithubService,例如:

public interface GithubService {
    @GET("repositories")
    Call<List<GithubRepo>> getAllRepositories();
}

现在我正在尝试将其他类注入RxBus

public interface Bus {

    void register(@NonNull Object observer);

    <T> CustomSubscriber<T> obtainSubscriber(@NonNull Class<T> eventClass, @NonNull Consumer<T> receiver);

    <T> void registerSubscriber(@NonNull Object observer, @NonNull CustomSubscriber<T> subscriber);

    void unregister(@NonNull Object observer);

    void post(@NonNull Object event);

}

进入作业文件,但我得到空

职业类:

public class GetLatestRepositories extends Job implements JobManagerInjectable {
    @Inject
    transient GithubService githubService;

    @Inject
    transient Bus eventBus;

    private Call<List<GithubRepo>> repositoryCall;

    public GetLatestRepositories() {
        super(new Params(JobPriority.MID).requireNetwork().persist());
        eventBus.register(this); /* IS NULL*/
    }

    @Override
    public void onAdded() {
    }

    ...

    @Override
    public void inject(ApplicationComponent component) {
        component.inject(this);
    }
}

ApplicationComponent

@ActivitiesScope
@Component(dependencies = GithubApplicationComponent.class)
public interface ApplicationComponent {
    void inject(ActivityRegister activityRegister);

    void inject(ActivityStartUpApplication activityStartUpApplication);

    void inject(GetLatestRepositories getLatestRepositories);
}

GithubApplicationComponent

@AlachiqApplicationScope
@Component(
        modules = {
                UserInformationModule.class,
                NetworkServiceModule.class,
                PicassoModule.class,
                JobManagerModule.class,
                RxModule.class,
                ActivityModule.class
        }
)
public interface GithubApplicationComponent {
    Picasso getPicasso();
    GithubService getGithubService();
    JobManager getJobManager();
    Bus getBus();
}

RxModule

@Module
public class RxModule {
    @Provides
    @AlachiqApplicationScope
    public Bus getBus() {
        return new RxBus();
    }
}

RxBux files

1 个答案:

答案 0 :(得分:2)

RxModule而不是:

return new RxBus();

执行:

return BusProvider.getInstance();