android - Dagger 2 和 Android 数据绑定(bind)适配器 : cannot be provided without an @Inject or @Provides

标签 android dagger-2 android-databinding

我正在使用 Android 的数据绑定(bind)库和 Dagger 2。我想利用 DatabingAdapter 在 RecyclerView 中显示我的图像。我使用 Dagger 创建了 Picasso 实例,并且必须将其注入(inject)到我创建的 DatabindingAdapter 中。我按照这个教程here我收到错误,如果没有 @Inject 或 @Provides 带注释的方法,则无法提供 Picasso。这是我的代码(简化了类以更多地关注这个问题,我之前已经让毕加索工作过)。

AppModule.java

@Module(includes = {AndroidInjectionModule.class, NetworkModule.class, ViewModelModule.class})
public class AppModule {

    @Provides
    @AppScope
    Picasso picasso(App app, OkHttp3Downloader okHttp3Downloader) {
        return new Picasso.Builder(app.getApplicationContext())
                .downloader(okHttp3Downloader)
                .indicatorsEnabled(true)
                .build();
    }
}

BindingModule.java

@Module
public class BindingModule {
    @BindingScope
    @Provides
    ImageBindingAdapter provideImageBindingAdapter(Picasso picasso) {
        return new ImageBindingAdapter(picasso);
    }
}

BindingComponent.java

@BindingScope
@Component(dependencies = AppComponent.class, modules = BindingModule.class)
public interface BindingComponent extends androidx.databinding.DataBindingComponent {
}

AppComponent.java

@AppScope
@Component(modules = {AppModule.class, AndroidSupportInjectionModule.class, ActivityBuildersModule.class})
public interface AppComponent {

    void inject(App app);

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(App application);

        AppComponent build();
    }
}

ImageBindingAdapter.java

public class ImageBindingAdapter {

    private final Picasso picasso;

    public ImageBindingAdapter(Picasso picasso) {
        this.picasso = picasso;
    }

    @BindingAdapter(value = "url")
    public void loadImageUrl(ImageView imageView, String url) {
        if (url != null && !url.trim().isEmpty())
            picasso.load(Constants.ENDPOINT + url).into(imageView);
    }
}

这就是错误所在。

error: [Dagger/MissingBinding] com.squareup.picasso.Picasso cannot be provided without an @Inject constructor or an @Provides-annotated method.
com.squareup.picasso.Picasso is injected at
com.ralphevmanzano.themoviedb.di.modules.BindingModule.provideImageBindingAdapter(picasso)
com.ralphevmanzano.themoviedb.databinding.ImageBindingAdapter is provided at
androidx.databinding.DataBindingComponent.getImageBindingAdapter()

非常感谢任何帮助。

最佳答案

您的 ImageBindingAdapter 类中缺少 @Inject 注释

@AppScope
public class ImageBindingAdapter {

    private final Picasso picasso;

    @Inject
    public ImageBindingAdapter(Picasso picasso) {
        this.picasso = picasso;
    }

    @BindingAdapter(value = "url")
    public void loadImageUrl(ImageView imageView, String url) {
        if (url != null && !url.trim().isEmpty())
            picasso.load(Constants.ENDPOINT + url).into(imageView);
    }
}

添加@Inject告诉dagger将此类添加到图表中,然后您可以将范围注释添加到您的类中以告知该类添加到哪个范围。

ps:您可以删除 BindingModule

关于android - Dagger 2 和 Android 数据绑定(bind)适配器 : cannot be provided without an @Inject or @Provides,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53001293/

相关文章:

android - ShowCaseView 圆圈大小

java - 无法滚动下面有 GridView 和 ListView 的屏幕

android - 尝试在 Android 的对话框中实现数据绑定(bind)

android - 非全屏 Activity

android - 下载 map 以供离线使用

android - Dagger 2 - 将 FragmentManager 传递给构造函数

android - 如何使用 Dagger2 将 Presenter 注入(inject) View(MVP 模式)

java - 带有 Kotlin 的 Dagger 2,在 ApplicationComponent 中返回具有泛型的类型

android - 从 fragment 中的变量进行数据绑定(bind),android?

java - Android数据绑定(bind)问题绑定(bind)适配器调用两次