dagger2找不到符号 [英] dagger2 cannot find symbol

查看:237
本文介绍了dagger2找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪 TODO 应用程序的 Dagger2 示例,但遇到2个错误。

I'm following the Dagger2 sample of TODO app but encounted with 2 errors.

Error1:找不到符号 DaggerNetComponent 。 (实际上在那里)

错误2:如果没有@provider注释的方法,就无法提供 Sharedpreference 。(我认为错误1的结果)

Error1: can't find symbol DaggerNetComponent. (Which is actually there)
Error2: Sharedpreference can't be provided without @provider-annotated method.(Which I think results from error1)

这是我冗长而简单的代码:
三个模块:

And here's my long but simple code:
Three modules:

@Module
public class AppModule {
    private final Application mApplication;

    AppModule(Application application) {
        mApplication = application;
    }

    @Provides
    Application provideApplication() {
        return mApplication;
    }
}

@Module
public class LoadingModule {
    public final LoadingContract.View mView;

    public LoadingModule(LoadingContract.View mView) {
        this.mView = mView;
    }

    @Provides
    LoadingContract.View provideLoadingContractView() {
        return mView;
    }
}

@Module
public class NetModule {

    @Provides @Singleton
    SharedPreferences providesSharedPreferences(Application application) {
        return PreferenceManager.getDefaultSharedPreferences(application);
    }
}


还有两个组成部分:


And two components:

@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
}

@FragmentScoped
@Component(dependencies = NetComponent.class, modules = LoadingModule.class)
public interface LoadingComponent {
    void inject(LoadingActivity loadingActivity);
}


我在MyApplication中获得了NetComponent:


I get NetComponent in MyApplication:

mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this))
                .netModule(new NetModule())
                .build();


和LoadingActivity中的LoadingComponent:


and LoadingComponent in LoadingActivity:

DaggerLoadingComponent.builder()
                .netComponent(((MyApplication)getApplication()).getNetComponent())
                .loadingModule(new LoadingModule(loadingFragment))
                .build()
                .inject(this);


我唯一想要的 LoadingComponent 要注入的是 LoadingPresenter

也在 LoadingActivity 中:

@Inject LoadingPresenter mLoadingActivityP; 


这就是 LoadingPresenter 构造而成:

public class LoadingPresenter implements LoadingContract.Presenter {
    private SharedPreferences sharedPreferences;
    private LoadingContract.View loadingView;

    @Inject
    public LoadingPresenter(LoadingContract.View loadingView, SharedPreferences sharedPreferences) {
        this.loadingView = loadingView;
        this.sharedPreferences = sharedPreferences;
    }

    @Inject
    void setupListeners() {
        loadingView.setPresenter(this);
    }

    public boolean checkLoginStatus() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        return sharedPreferences.getBoolean("loginStatus", false);
    }

    @Override
    public void start() {
    }
}


程序结束。

几天来一直困扰着我。会得到任何帮助。


End of my program.
It has been annoying me for several days. Any help is appreciated.

推荐答案

您会发现错误的因果关系。 Dagger * 类是注释处理器的最终输出。因为您的图形中有一个错误,所以Dagger处理器无法完成,因此输出丢失。

You have the causality of your errors backwards. The Dagger* classes are the final output of the annotation processor. Because you had an error in your graph, the Dagger processor couldn't complete, so the output was missing.

第二个错误是您在请求<$ c来自未绑定的组件中的$ c> SharedPreferences 。由于您选择的是组件依赖关系而不是子组件,因此依赖项中的所有绑定都不继承。为了使您的 LoadingComponent 使用来自 NetComponent 的绑定,它们必须在组件接口上公开。将 SharedPreferences sharedPreferences(); 添加到 NetComponent 或切换到子组件应该可以解决您的问题。

The second error is saying that you are requesting SharedPreferences from within a component that doesn't have it bound. Because you've chosen component dependencies rather than subcomponents, all of the bindings from your dependencies are not inherited. In order for your LoadingComponent to use bindings from NetComponent they must be exposed on the component interface. Adding SharedPreferences sharedPreferences(); to NetComponent or switching to subcomponents should fix your problem.

请参见 @组件文档和用户指南中子组件的页面了解更多信息。

See the @Component docs and the page on subcomponents in the user's guide for more information.

这篇关于dagger2找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆