2015-10-18 70 views
1

我有一段時間搞清楚如何設置Dagger依賴關係來滿足本質上這個片段。Dagger Android查看注入錯誤

我有一個控制器WelcomeScreen(它擴展了Path - 一個Mortar/Flow的東西),它聲明它注入了一個ThingView。然後我注入一個ThingView的提供者,我的ThingView構造函數有一個@Inject註釋和一個Activity,我在別處提供。

我仍然最終得到這個錯誤:沒有註冊註冊成員/ com ..... view.ThingView。您必須將其明確添加到您的某個模塊中的「注入」選項。

關於我失蹤的想法?

public class WelcomeScreen extends Path { 
    @dagger.Module(
     injects = { 
      ThingView.class, 
     }, 
     addsTo = ActivityModule.class) 
    public class Module { 
    } 

    @Singleton 
    public static class Presenter extends ViewPresenter<WelcomeView> { 
    @Inject 
    public Presenter(
     Provider<ThingView> thingViewProvider,) { 
     // This causes an error: No inject registered for members/com.....view.ThingView. 
     // You must explicitly add it to the 'injects' option in one of your modules. 
     thingViewProvider.get() 
    } 
    } 
} 

public class ThingView extends LinearLayout { 
@Inject 
    public ThingView(Activity activity) { 
    super(activity); 
    init(activity); 
    } 

    // Note - this might not be used anywhere. 
    public ThingView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
    } 

    private void init(Context context) { 
    ObjectGraphService.inject(context, this); 
    View view = View.inflate(context, R.layout.view_thread_pack, this); 
    ButterKnife.inject(view); 
    } 
} 

更新:我也嘗試添加下面這已對錯誤消息沒有影響:

@Module(addsTo = ApplicationModule.class, library = true) 
public class ActivityModule { 
    @Provides 
    public ThreadPackView providesThreadPackView() { 
    return new ThreadPackView(activity); 
    } 
} 

回答

0

那砂漿倒不怎麼工作的。您無法將視圖注入演示者。相反,您將您的演示者注入您的視圖。下面是我發現,AA少數樣本顯示了一個類似的設置到你正在做https://github.com/Zhuinden/MortarFlowInitialDemohttps://github.com/matthew-compton/NerdRoll

+0

什麼在這種情況下,這是不以我的屏幕/主持人,這是一個觀點,我想創建很多時候我的演講取得了一些的東西。 – haberdasher

+0

嗯。您需要使用背景上的佈局充氣器來創建視圖。如果您將上下文傳遞到模塊中,該模塊可以在提供的方法中使視圖膨脹。 – FriendlyMikhail

+0

雖然膨脹需要佈局,對吧?這意味着這個誇大觀點的階層需要知道如何誇大它,我不喜歡。 – haberdasher