简体   繁体   English

@Inject for构造函数的问题

[英]Issue with @Inject for Constructor

I am new to robojuice, but i need to work on a piece of code which was already built by someone else. 我是robojuice的新手,但是我需要处理已经由其他人构建的一段代码。 I am facing issue if I add an extra parameter to the constructor of a class which already has @Inject. 如果将额外的参数添加到已经具有@Inject的类的构造函数中,我将面临问题。 My android application crashes giving the below error with no detail description about the issue: 我的android应用程序崩溃,给出以下错误,但未对此问题进行详细说明:

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app.envTest/com.my.app.navigation.NavigActivity}: com.google.inject.ConfigurationException: Guice configuration errors:"

I am sure this error is not with the activity, but with the new parameter i added to the constructor. 我确定此错误不是由于活动引起的,而是由于我将新参数添加到了构造函数中。 If i remove that param it works fine. 如果我删除该参数,它将正常工作。

Previously: 先前:

 @Inject
public PlotRepo(RuntimeExceptionDao<Plot, String> plotDao, RuntimeExceptionDao<LocalPlotData, Long> localPlotDataDao) {
    this.plotDao = plotDao;
    this.localPlotDataDao = localPlotDataDao;
   }

Facing issue for: 面临的问题:

 @Inject
public PlotRepo(RuntimeExceptionDao<Plot, String> plotDao, RuntimeExceptionDao<LocalPlotData, Long> localPlotDataDao, RuntimeExceptionDao<LocalSelPlotData, Long> localSelPlotDataDao) {
    this.plotDao = plotDao;
    this.localPlotDataDao = localPlotDataDao;
    this.localSelPlotDataDao = localSelPlotDataDao;
   }

After Debugging i got this error: 调试后出现此错误:

1) Could not find a suitable constructor in     com.j256.ormlite.dao.RuntimeExceptionDao. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at com.j256.ormlite.dao.RuntimeExceptionDao.class(Unknown Source)
  while locating     com.j256.ormlite.dao.RuntimeExceptionDao<com.myapp.s.b.sets.domain.LocalSelPlotData, java.lang.Long>
  for parameter 2 at com.tp.my.sets.PlotRepo.<init>(Unknown Source)
  while locating com.myapp.s.b.sets.PlotRepo
  for parameter 1 at com.myapp.s.b.GroupingManager.<init>(Unknown Source)
  while locating com.myapp.s.b.GroupingManager
  for field at com.myapp.s.b.navigation.NavigActivity.groupingManager(Unknown    Source)
   while locating com.myapp.s.b.navigation.NavigActivity

Not sure where I am going wrong, also could not find much help on this. 不知道我要去哪里错了,也找不到很多帮助。 Can someone help me figure out the issue. 有人可以帮我解决这个问题。

Thanks 谢谢

There were two things that missed in my code. 我的代码中遗漏了两件事。 My database classes were missing default constructors. 我的数据库类缺少默认构造函数。 Ormlite needs default constructors, which were missing in some of my classes. Ormlite需要默认的构造函数,而在我的某些类中却没有。 After that, I had missed the bindings for the Db annotated dependency like this: 之后,我错过了这样的Db注释依赖项的绑定:

    bind(new TypeLiteral<RuntimeExceptionDao<MyTable, Long>>() {}).toProvider(new DaoProvider<MyTable, Long>(MyTable.class));

After these changes the issue got fixed. 这些更改后,问题已解决。

Thanks all for the help! 谢谢大家的帮助!

Firstly - though this does not explicitly answer your question, but cannot have those two constructors - after Type Erasure they are identical and indistinguishable. 首先-尽管这并未明确回答您的问题,但不能拥有这两个构造函数-类型擦除后,它们是相同的并且无法区分。

You'd need to explicitly define classes such as 您需要明确定义类,例如

RuntimePlotExceptionDao extends RuntimeExceptionDao<Plot, String>
RuntimeLocalPlotDateExceptionDao extends RuntimeExceptionDao<LocalPlotData, Long>

Secondly - why do you want to have two @Inject constructors? 其次-为什么要有两个@Inject构造函数? Which one should the container pick when creating the object? 容器在创建对象时应选择哪一个?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM