[英]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:有两种解决方案:
@Provides
for String
.String
创建另一个@Provides
。 This is not recommended as you'll run into issue if another String
type is required.String
类型,您会遇到问题。@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.