java - Android Dagger 报错找不到符号类 DaggerDashboardComponent

标签 java android android-studio dependency-injection dagger

我正在处理一个使用 Dagger 的现有项目。

这是我第一次尝试使用 Dagger ,对此我有点不知所措。

我已尝试从现有 fragment/模块/组件复制所有现有类、接口(interface)和其他所有内容,但出现以下错误:

Error:(7, 42) 错误:找不到符号类 DaggerDashboardComponent

我尝试了Clean ProjectRebuild Project,我尝试了Invalidate Caches and Restart,我从 Android 的编译器设置中启用了注释功能Studio,我尝试关闭项目并重新打开它,但由于某种原因(可能是类生成中的一些错误),我无法编译,因为 dagger 没有生成所需的类。

请在下面找到我创建的所有类/接口(interface):

fragment 仪表板:

package com.example.app.ui.dashboard;

import com.example.app.R;
import com.example.app.base.AbstractRequestFragment;
import com.example.app.databinding.FragmentDashboardBinding;
import com.example.app.di.component.AppComponent;
import com.example.app.di.component.DaggerDashboardComponent;
import com.example.app.di.module.DashboardModule;
import com.example.app.presenter.dashboard.DashboardPresenter;
import com.example.app.ui.dashboard.DashboardView;

import javax.inject.Inject;

import retrofit2.Call;

/**
 * Created by on 31-05-2017.
 */
public class FragmentDashboard extends AbstractRequestFragment<FragmentDashboardBinding> implements DashboardView {

    @Inject
    DashboardPresenter dashboardPresenter;

    @Override
    protected int layoutToInflate() {
        return R.layout.fragment_dashboard;
    }

    @Override
    protected void injectComponent(AppComponent appComponent) {
        DaggerDashboardComponent.builder()
                .appComponent(appComponent)
                .dashboardModule(new DashboardModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initializePresenter(FragmentDashboardBinding fragmentDashboardBinding) {
        dashboardPresenter.initialize(fragmentDashboardBinding);
    }

    @Override
    public void addRequestToStack(Call<?> call) {
        addRequest(call);
    }
}

仪表板组件:

package com.example.app.di.component;

import com.example.app.di.module.DashboardModule;
import com.example.app.di.scope.Fragment;
import com.example.app.presenter.dashboard.DashboardPresenter;
import com.example.app.ui.dashboard.DashboardView;
import com.example.app.ui.dashboard.FragmentDashboard;

import dagger.Component;

/**
 * Created by on 31-05-2017.
 */
@Fragment
@Component(modules = DashboardModule.class, dependencies = AppComponent.class)
public interface DashboardComponent {

    /* BASE */
    void inject(FragmentDashboard fragmentDashboard);

    /* DASHBOARD */
    DashboardView providesDashboardView();
    DashboardPresenter providesDashboardPresenter();

}

仪表板模块:

package com.example.app.di.module;

import com.example.app.di.scope.Fragment;
import com.example.app.ui.dashboard.DashboardView;

import dagger.Module;
import dagger.Provides;

/**
 * Created by on 31-05-2017.
 */
@Module
public class DashboardModule {

    private DashboardView view;

    public DashboardModule(DashboardView view) {
        this.view = view;
    }

    @Fragment
    @Provides
    public DashboardView providesDashboardView() {
        return view;
    }


}

仪表板演示者:

package com.example.app.presenter.dashboard;

import com.example.app.base.BasePresenter;
import com.example.app.databinding.FragmentDashboardBinding;
import com.example.app.databinding.FragmentLoginBinding;

/**
 * Created by on 31-05-2017.
 */
public interface DashboardPresenter extends BasePresenter<FragmentDashboardBinding> {
}

DashboardPresenterImpl:

package com.example.app.presenter.dashboard;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;

import com.massivedisaster.activitymanager.ActivityFragmentManager;
import com.example.app.api.ApiListener;
import com.example.app.base.ActivityFullScreen;
import com.example.app.databinding.FragmentDashboardBinding;
import com.example.app.databinding.FragmentLoginBinding;
import com.example.app.presenter.dashboard.DashboardPresenter;
import com.example.app.ui.login.LoginView;
import com.example.app.ui.main.FragmentMain;
import com.example.app.util.ApiManager;
import com.example.app.util.OnOneClickListener;
import com.example.app.util.OnTouchListener;
import com.example.app.util.SnackbarHandler;
import com.example.app.util.ViewHelper;
import com.example.app.util.factory.LoginFactory;

import java.util.ArrayList;

/**
 * Created by on 31-05-2017.
 */
public class DashboardPresenterImpl implements DashboardPresenter {

    private ApiManager apiManager;
    private FragmentDashboardBinding fragmentBinding;
    private LoginFactory loginFactory;
    private LoginView loginView;
    private ViewHelper viewHelper;

    public DashboardPresenterImpl(ApiManager apiManager, LoginFactory loginFactory, LoginView loginView, ViewHelper viewHelper) {
        this.apiManager = apiManager;
        this.loginFactory = loginFactory;
        this.loginView = loginView;
        this.viewHelper = viewHelper;
    }

    @Override
    public void initialize(FragmentDashboardBinding fragmentBinding) {
        this.fragmentBinding = fragmentBinding;

        this.fragmentBinding.btnLogin.setOnClickListener(new OnLoginClickListener());

        // apply on touch cleanup to view and all elements within view
        ArrayList<View> touchables = fragmentBinding.getRoot().getTouchables();
        touchables.add(fragmentBinding.getRoot());
        for (View v : touchables) {
            v.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return super.onTouch(view, motionEvent);
                }
            });
        }
    }


    // main login listener
    private class OnLoginClickListener extends OnOneClickListener {

        @Override
        public void doOnClick(View view) {
            Intent intent = ActivityFragmentManager.getIntent(loginView.getActivity(), ActivityFullScreen.class, FragmentMain.class);

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

            loginView.getActivity().startActivity(intent);
        }
    }


}

仪表板 View :

package com.example.app.ui.dashboard;

import com.example.app.base.RequestView;

/**
 * Created by on 31-05-2017.
 */
public interface DashboardView extends RequestView {

}

fragment 仪表板:

package com.example.app.ui.dashboard;

import com.example.app.R;
import com.example.app.base.AbstractRequestFragment;
import com.example.app.databinding.FragmentDashboardBinding;
import com.example.app.di.component.AppComponent;
import com.example.app.di.component.DaggerDashboardComponent;
import com.example.app.di.module.DashboardModule;
import com.example.app.presenter.dashboard.DashboardPresenter;
import com.example.app.ui.dashboard.DashboardView;

import javax.inject.Inject;

import retrofit2.Call;

/**
 * Created by on 31-05-2017.
 */
public class FragmentDashboard extends AbstractRequestFragment<FragmentDashboardBinding> implements DashboardView {

    @Inject
    DashboardPresenter dashboardPresenter;

    @Override
    protected int layoutToInflate() {
        return R.layout.fragment_dashboard;
    }

    @Override
    protected void injectComponent(AppComponent appComponent) {
        DaggerDashboardComponent.builder()
                .appComponent(appComponent)
                .dashboardModule(new DashboardModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initializePresenter(FragmentDashboardBinding fragmentDashboardBinding) {
        dashboardPresenter.initialize(fragmentDashboardBinding);
    }

    @Override
    public void addRequestToStack(Call<?> call) {
        addRequest(call);
    }
}

谁能告诉我这里缺少什么?

最佳答案

  • 您是否在 gradle 中添加了 dagger 预处理?例如,apt 'com.google.dagger:dagger-compiler:2.x'

  • 您应该检查编译器日志。如果 dagger 发现错误,则所有 dagger 编译都会失败。 Dagger 不太友好地详细说明编译器错误,但您应该在 dagger 文件中看到一个错误列表,其中一个(通常是第一个)应该会告诉您 dagge 没有生成类的真正原因。

关于java - Android Dagger 报错找不到符号类 DaggerDashboardComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617479/

相关文章:

java - 如何从另一个类添加值(java)

java - 如何将Repository自动生成的ID返回到MainActivity?

java - 为什么setTypeFace()无法解析?

android - Facebook 三星 S3 权限屏幕

java - 如何仅在运行junit时从项目类路径中删除依赖项?

java - 如何使用 SWT 将双击事件监听器添加到托盘图标?

android - 从 AndroidManifeast.xml 的元数据访问长 key

java - 使用 maven 存储库将 java 库添加到 Android Studio 项目

android - 是否可以在 Android Studio 项目中集成 Dagger 和 AspectJ?

android - Gradle构建任务表示Gradle Wrapper版本低于Gradle属性中定义的版本