添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
果断的大脸猫  ·  全屏指南 - Web API | MDN·  1 月前    · 
大气的日记本  ·  Eclipse RCP应用pdf ...·  1 年前    · 
焦虑的面包  ·  c++ - C 未处理的异常 - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I try to implement Dagger2 into my Kotlin project but I have problems with the @Inject annotation.

In Java it looks like this and this works fine:

public class FooActivity extends Activity {
    @Inject
    @Named("accessTokenObservable")
    public Flowable<Optional<AccessToken>> accessTokenObservable;
    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.getGraph().inject(this);

But how I have to write the @Inject line in Kotlin?

When I use this one:

@Inject
@Named("accessTokenObservable")
var accessTokenObservable: Flowable<Optional<AccessToken>>? = null

I get this error message:

Error:Dagger does not support injection into private fields

If I use lateinit:

@Inject
@Named("accessTokenObservable")
lateinit var accessTokenObservable: Flowable<Optional<AccessToken>>

I get this error message:

Error:Flowable<Optional<AccessToken>> cannot be provided without
an @Provides- or @Produces-annotated method.

What is the right syntax to inject something in Kotlin?

lateinit var presenter: ItemsPresenter override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState)

Without any problems

Slightly off-topic nitpick but according to google.github.io/dagger/android.html ''AndroidInjection.inject(this);'' should be called before ''super.onCreate(savedInstanceState);'' – activout.se Oct 9, 2017 at 8:46

You have to change your injection code like this:

@field:[Inject Named("accessTokenObservable")]
lateinit var accessTokenObservable: Flowable<Optional<AccessToken>>

, and it will be fixed.

Do you provide Flowable<Optional<AccessToken>> accessTokenObservable somewhere in your code?

If not Error:Flowable<Optional<AccessToken>> cannot be provided without an @Provides- or @Produces-annotated method. can be produced. Because you are trying to inject object without provide it. In this case you need to provide in your injector class:

@Provide
@Named("accessTokenObservable")
fun provideAccessTokenObservable : Flowable<Optional<AccessToken>>{
    return yourAccessTokenObservable

then you need to inject your object in your activity

@Inject
@Named("accessTokenObservable")
lateinit var accessTokenObservable: Flowable<Optional<AccessToken>>
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.