2014-10-08 85 views
0

我是新來的匕首,並試圖找出爲什麼不注入一個特定的依賴關係。當試圖在一個片段中注入演示,我發現了以下錯誤:「即使我聲明它沒有註冊註冊類」

Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.company.myapp.view.fragment.OverviewFragment. You must explicitly add it to the 'injects' option in one of your modules. 
      at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:302) 
      at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279) 
      at com.company.myapp.view.fragment.OverviewFragment.initializePresenter(OverviewFragment.java:50) 
      at com.company.myapp.view.fragment.BaseFragment.onViewCreated(BaseFragment.java:28) 
      at com.company.myapp.view.fragment.OverviewFragment.onViewCreated(OverviewFragment.java:84) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:871) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040) 
      at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142) 
      at android.app.Activity.onCreateView(Activity.java:4786) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
            at android.app.Activity.setContentView(Activity.java:1929) 
            at com.company.myapp.view.activity.HomeActivity.onCreate(HomeActivity.java:23) 
            at android.app.Activity.performCreate(Activity.java:5231) 

...

我有拉動必要的應用級模塊的ApplicationModule(如分析)。

@Module(
     injects = { 
       BaseApplication.class, 
       MyApplication.class, 
       TestMyApplication.class 
     }, 
     includes = { DomainModule.class } 
) 
public class MyModule { 
    private final BaseApplication mApp; 

    public MyModule(BaseApplication app) { 
     mApp = app; 
    } 

    @Provides 
    @Singleton 
    public Context provideApplicationContext() { 
     return mApp; 
    } 

} 

我有一個OverviewModel直接關聯到OverviewFragment。此模塊的全部目的是將Presenter插入到OverviewFragment中。

@Module(
     injects = OverviewFragment.class, 
     addsTo = ReviewsModule.class 
) 
public class OverviewModule { 

    private OverviewView mView; 

    public OverviewModule(OverviewView view) { 
     mView = view; 
    } 

    @Provides 
    @Singleton 
    public OverviewView provideView() { 
     return mView; 
    } 

    @Provides 
    @Singleton 
    public OverviewPresenter provideOverviewPresenter(OverviewView view) { 
     return new OverviewPresenter(view); 
    } 

} 

對象圖是在BaseApplication對象的OnCreate(與評價模塊)中創建並注射(Modules.list()返回模塊的列表,目前爲空白TestReviewsModule和ReviewsModule ...上方觀察)。

private void initializeObjectGraph() { 
    Timber.d("Initializing application object graph!"); 
    og = ObjectGraph.create(Modules.list(this)); 
    og.inject(this); 
} 

在OverviewFragment,我檢索此創建對象圖(onViewCreated),並執行以下操作來我OverviewModule添加到應用程序範圍的對象圖:

public class OverviewFragment extends BaseFragment implements OverviewView { 

    protected ObjectGraph og; 
    @Inject OverviewPresenter mPresenter; 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     og = BaseApplication.getInstance().getAppObjectGraph(); 
     initializePresenter(); 
    } 

    @Override 
    void initializePresenter() { 
     og.plus(new OverviewModule(this)); 
     og.inject(this); 
     mPresenter.initialize(mVendorId); 
    } 
} 

爲什麼我的應用程序說我的天堂即使我明確聲明我正在向OverviewModule中的OverviewFragment注入內容,也沒有註冊?

回答

6

匕首/ DI故障排除是一種困難,所以我會盡可能徹底......也許這會幫助其他人。

我在閱讀其他一些SO問題(如Dagger can't find injectable members on a module)和一些文檔後發現問題。

我的基本問題是缺乏對.plus()方法的理解。我沒有意識到它返回一個新的ObjectGraph,而不是修改它被調用的圖形。通過做一個.plus(),然後注入相同的ObjectGraph我打電話.plus(),我基本上試圖從我的評論模塊注入...它沒有OverviewFragment的知識,因此錯誤。

在OverviewFragment我需要從做出改變:

// old way 
void initializePresenter() { 
    og.plus(new OverviewModule(this)); // Note this line 
    og.inject(this); 
    mPresenter.initialize(mVendorId); 
} 

// new way 
void initializePresenter() { 
    og = og.plus(new OverviewModule(this)); // Note this line 
    og.inject(this); 
    mPresenter.initialize(mVendorId); 
} 

這將返回一個新的圖形既OverviewModule和ReviewsModule。

我也不需要爲OverviewModule添加addsTo = ReviewsModule.class註釋屬性。根據我的理解,.plus()是此的運行時版本(請參閱前面的鏈接問題)。

+0

嗨,只是對刪除線中的文字感興趣。這是爲什麼? – lemuel 2015-03-10 06:00:42