2016-12-19 81 views
0

我正在嘗試用Dagger設置一個項目。現在,當我建立,爲組件生成任何匕首的*類,我得到了以下錯誤:Dagger不會爲Android生成Dagger類

Error:(10, 8) error: [com.redditapp.dagger.RedditAppGraph.inject(com.redditapp.RedditApplication)] 
com.redditapp.ui.ActivityHierarchyServer cannot be provided without an @Provides- or @Produces-annotated method. 
com.redditapp.ui.ActivityHierarchyServer is injected at 
com.redditapp.RedditApplication.activityHierarchyServer 
com.redditapp.RedditApplication is injected at 
com.redditapp.dagger.RedditAppGraph.inject(app) 

Error:(13, 10) error: com.redditapp.base.navigation.activity.ActivityScreenSwitcher cannot be provided without an @Inject constructor or from an @Provides-annotated method. 
com.redditapp.base.navigation.activity.ActivityScreenSwitcher is injected at 
com.redditapp.ui.screens.home.HomePresenter.<init>(screenSwitcher) 
com.redditapp.ui.screens.home.HomePresenter is injected at 
com.redditapp.ui.screens.home.HomeActivity.presenter 
com.redditapp.ui.screens.home.HomeActivity is injected at 
com.redditapp.ui.screens.home.HomeComponent.inject(activity) 

這裏是我的當前設置。

應用程序級類使用:

public void buildComponentAndInject() { 
     component = RedditAppComponent.Initializer.init(this); 
     component.inject(this); 
    } 

的RedditAppComponent看起來像這樣,也不產生DaggerRedditAppComponent,因此是紅色:

@ApplicationScope 
@Component(modules = { RedditAppModule.class, UiModule.class}) 
public interface RedditAppComponent extends RedditAppGraph { 
    /** 
    * An initializer that creates the graph from an application. 
    */ 
    final class Initializer { 
     public static RedditAppComponent init(RedditApplication app) { 
      return DaggerRedditAppComponent.builder() 
        .redditAppModule(new RedditAppModule(app)) 
        .uiModule(new UiModule()) 
        .build(); 
     } 
     private Initializer() {} // No instances. 
    } 
} 

與父類RedditAppGraph:

public interface RedditAppGraph { 
    void inject(RedditApplication app); 
    ViewContainer viewContainer(); 
    ActivityHierarchyServer activityHierarchyServer(); 
} 

包含提供者方法的UI模塊是throwin g的錯誤看起來像︰

@Module 
public class UiModule { 

    @Provides 
    @ApplicationScope 
    ActivityScreenSwitcher provideActivityScreenSwitcher() { 
     return new ActivityScreenSwitcher(); 
    } 

    @Provides 
    @ApplicationScope 
    @ActivityScreenSwitcherServer 
    ActivityHierarchyServer provideActivityHierarchyServer(final ActivityScreenSwitcher screenSwitcher) { 
     return new ActivityHierarchyServer.Empty() { 
      @Override 
      public void onActivityStarted(Activity activity) { 
       screenSwitcher.attach(activity); 
      } 

      @Override 
      public void onActivityStopped(Activity activity) { 
       screenSwitcher.detach(); 
      } 
     }; 
    } 
} 

我試過重建項目,看看它是否會生成類。

+0

你在應用編譯器嗎?你是否在你的build gradle中使用了'apt「com.google.dagger:dagger-compiler:2.8」''? – Fred

+0

我這樣做,但不是apt我正在使用annotationProcessor。 – AggieDev

回答

2

看起來像ActivityScreenSwitcherServer是一個限定符。如果是這樣的話,您將綁定您的ActivityHierarchyServer與限定符,但在您的組件(通過RedditAppGraph)暴露ActivityHierarchyServer沒有它。從您的@Provides方法中刪除限定符或將其添加到組件接口。

+0

真棒,感謝格雷戈裏!像魅力一樣工作。 – AggieDev

+2

FWIW,我們計劃在錯誤信息中給你「建議的修復」,但我們還沒有完成。 – gk5885