简体   繁体   English

如何解决 dagger 错误 - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供字符串

[英]How to resolve dagger error - String cannot be provided without an @Inject constructor or an @Provides-annotated method

Hi I have a PdfTicketStorage class which has a constructor which accepts 2 parameters, I have added @Inject to the constructor and also it in the MockServiceOrderModule.嗨,我有一个 PdfTicketStorage class,它有一个接受 2 个参数的构造函数,我已将 @Inject 添加到构造函数中,并将其添加到 MockServiceOrderModule 中。 but I get an error但我得到一个错误

MockAppComponent.java:48: error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.

full error is完全错误是

/home/circleci/code/app/src/androidTest/java/com/xx/xxx/test/injection/components/MockAppComponent.java:48: error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.
public interface MockAppComponent extends AppComponent {
       ^
      java.lang.String is injected at
          com.xxx.xx.test.injection.modules.MockServiceOrderModule.providePdfTicketStorage(cloudStorageBucket, …)
      com.xxx.xx.printing.storage.PdfTicketStorage is injected at
          com.xxx.xx.clean.offline.OfflineHelper(…, pdfTicketStorage, …)
      com.xxx.xx.clean.offline.OfflineHelper is injected at
          com.xxx.xx.clean.offline.repository.OfflineUserApiRepository(…, offlineHelper)
      com.xxx.xx.clean.offline.repository.OfflineUserApiRepository is injected at
          com.xxx.xx.clean.florder.data.repository.flOrderRepository(offlineUserApiRepository, …)
      com.xxx.xx.clean.florder.data.repository.flOrderRepository is injected at
          com.xxx.xx.test.scenarios.BaseScenario.flOrderRepository
      com.xxx.xx.test.scenarios.BaseScenario is injected at
          com.xxx.xx.test.injection.components.MockAppComponent.inject(com.xxx.xx.test.scenarios.BaseScenario)
  The following other entry points also depend on it:
      com.xxx.xx.test.injection.components.MockAppComponent.inject(com.xxx.xx.test.scenarios.VehicleTakeoverScenario)
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.activities.MainActivity) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.fragments.fller.florder.BaseflOrderFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.fragments.fller.florder.BaseflOrderDataFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.fragments.fller.florder.flOrderDataFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.fragments.fller.florder.flOrderSummaryFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.fragments.fller.florder.flOrderDeallocationFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.clean.orderslist.serviceorders.view.ServiceOrderListFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.fragments.fller.florder.ServiceOrderViewFragment) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      com.xxx.xx.injection.components.ActivityComponent.inject(com.xxx.xx.clean.orderslist.serviceorders.view.ServiceOrderAdapter) [com.xxx.xx.test.injection.components.MockAppComponent → com.xxx.xx.injection.components.ActivityComponent]
      and 31 others/home/circleci/code/app/src/androidTest/java/com/xx/xxx/test/injection/components/MockAppComponent.java:48: error: [Dagger/MissingBinding] java.lang.String cannot be provided without an @Inject constructor or an @Provides-annotated method.

PdfTicketStorage PdfTicketStorage

@PerApp
public class PdfTicketStorage {

    private final String cloudStorageBucket;
    private final Lazy<OfflineHelper> offlineHelper;

    @Inject
    public PdfTicketStorage(String cloudStorageBucket, Lazy<OfflineHelper> offlineHelper) {
        this.cloudStorageBucket = cloudStorageBucket;
        this.offlineHelper = offlineHelper;
    }
}

OfflineHelper离线助手

@PerApp
class OfflineHelper @Inject constructor(
    private val app: App,
    private val settings: Settings,
    private val pdfTicketStorage: PdfTicketStorage,
    @Named("default") private val gson: Gson
) 

MockServiceOrderModule模拟服务订单模块

@Module
public class MockServiceOrderModule {

      @Provides @PerApp
    PdfTicketStorage providePdfTicketStorage(String cloudStorageBucket, Lazy<OfflineHelper> offlineHelper) {
        return new PdfTicketStorage(cloudStorageBucket, offlineHelper);
    }

}

Could you suggest what am I doing wrong here please你能建议我在这里做错什么吗?

your suggestions on how can I resolve this please this would be very helpful您对如何解决此问题的建议,这将非常有帮助

thanks R谢谢 R

Dagger's acyclic graph does not know how to provide the type String , in the case of String cloudStorageBucket . Dagger 的无环图不知道如何提供String类型,在String cloudStorageBucket的情况下。 There are two solutions:有两种解决方案:

  1. Either create another @Provides for String .String创建另一个@Provides This is not recommended as you'll run into issue if another String type is required.不建议这样做,因为如果需要其他String类型,您会遇到问题。
  2. (Recommended) Mark the @Provides in the 1 with @Named("cloudStorageBucket") . (推荐)用@Named("cloudStorageBucket")标记 1 中的@Provides

You can also use @assisted-inject if this String will be provided at runtime.如果此String将在运行时提供,您也可以使用@assisted-inject

First of all, When you are already providing the object here in this dagger module:首先,当您已经在此匕首模块中提供 object 时:

@Module
public class MockServiceOrderModule {

      @Provides @PerApp
    PdfTicketStorage providePdfTicketStorage(String cloudStorageBucket, Lazy<OfflineHelper> offlineHelper) {
        return new PdfTicketStorage(cloudStorageBucket, offlineHelper);
    }

}

You don't need to use constructor injection in PdfTicketStorage class.您不需要在PdfTicketStorage class 中使用构造函数注入。 You can just inject the object and use then like this:您可以只注入 object 然后像这样使用:

@PerApp
public class PdfTicketStorage {

    @Inject
    public PdfTicketStorage pdfTicketStorage;
}

But also you have to make sure to provides the string in dagger module also.但你也必须确保在 dagger 模块中提供字符串。 Not sure why you need to provide the string in dagger module?不确定为什么需要在 dagger 模块中提供字符串?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dagger 2错误:如果没有@inject构造函数或@ provide-annotated方法,则无法提供 - Dagger 2 error: cannot be provided without an @inject constructor or from an @provides-annotated method Dagger2-没有@Inject构造函数或@Provides注释方法无法提供 - Dagger2 - cannot be provided without an @Inject constructor or from an @Provides-annotated method 没有@Inject构造函数或@Provides注释方法无法提供-Dagger 2 - cannot be provided without an @Inject constructor or from an @Provides-annotated method - Dagger 2 dagger2错误“没有@Inject构造函数或来自@ Provide-annotated方法,不能提供android.app.Application” - dagger2 error “android.app.Application cannot be provided without an @Inject constructor or from an @Provides-annotated method” 如何修复 - 如果没有 @Inject 构造函数或 @Provides-annotated 方法,则无法提供 - How to fix - cannot be provided without an @Inject constructor or an @Provides-annotated method 如何修复 dagger2 库中的“如果没有 @Provides-annotated 方法就无法提供”错误 - How to fix “cannot be provided without an @Provides-annotated method” error in dagger2 library 不能在没有 @Provides 注释的方法的情况下提供(Android Kotlin 中的 Dagger 2) - Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin) Dagger 2-如果没有@Provides注释的方法,则无法提供 - Dagger 2 - Cannot be provided without an @Provides-annotated method 如果没有@ Provide-annotated方法,则无法提供Dagger 2 - Dagger 2 cannot be provided without an @Provides-annotated method Dagger 2:没有 @Provides 注释的方法就无法提供 - Dagger 2: Cannot be provided without an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM