如何在匕首2中注入otto总线对象

时间:2018-09-06 13:36:06

标签: android dagger-2 otto

我为dagger2创建了一个应用模块

@Module
public class VachakaModule {
private final Application mApplication;
private final String APPLICATION_KEY = "Vachaka";
private Bus bus;

public VachakaModule(Application mApplication) {
    this.mApplication = mApplication;
    this.bus = new Bus();
}

@Provides
Application applicationContext() {
    return mApplication;
}


@Provides
SharedPreferences sharedPreferences() {
    return mApplication.getSharedPreferences(APPLICATION_KEY, Context.MODE_PRIVATE);
}

@Provides
Bus getBus() {
    return bus;
}
}

和应用程序类的oncreate为

@Override
public void onCreate() {
    super.onCreate();
    vachaka = this;
    vachakaComponent = DaggerVachakaComponent.builder()
            .vachakaModule(new VachakaModule(this))
            .build();

}

现在,我希望可以在我的Viewholder类之一中访问总线对象

public static class SimpleNewsViewHolder extends MyViewHolder {

    @Inject
    Bus bus;


    @BindView(R.id.buttonMore)
    Button buttonMore;


    public SimpleNewsViewHolder(View itemView) {
        super(itemView);
    }

    @Override
    void bind(NewsModel.News item) {
            buttonMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bus.post(new LoadNewsEvent(news_id));
            }
        });
    }
}

我遇到错误尝试在bus.post行中的空对象引用上调用虚拟方法'void com.squareup.otto.Bus.post(java.lang.Object)'。

如何将这个总线对象正确地插入到viewholder类中?

感谢阅读

0 个答案:

没有答案