2016-11-10 57 views
0

我开发使用数据绑定,但之后增加Dagger2和执行模块,组件和移植物我面临这个错误的一个项目:误差DataDinding和Dagger2

Error:(8, 74) error: package com.anda.soft.app.databinding does not
exist
Error:(16, 13) error: cannot find symbol class ActivityMainBinding
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: not a valid component method: injectPresentationFragmentPresenter()

这是我的模块:

@Module 
public class Modul { 

    private Context context; 


    public Modul(Context context){ 
     this.context = context; 
    } 

    @Provides 
    public MainActivity provideMainActivity(){ 
     return new MainActivity(); 
    } 

    @Provides 
    public PresentationFragmentPresenter providePresentationFragment(){ 
     return new PresentationFragmentPresenterImp(provideMainActivity()); 
    } 

} 

我的成分,它:

@Component (modules = Modul.class) 

public interface Compoment { 
void injectPresentationFragmentPresenter(); 
} 

和图表

public class App extends Application { 

    private Compoment mCompoment; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     setUpGraph(); 
    } 

    private void setUpGraph() { 
     mCompoment = DaggerCompoment.builder() 
       .modul(new Modul(this)) 
       .build(); 
    } 


    public Compoment getCompoment(){ 
     return mCompoment; 
    } 
} 

最后我MainActivity

private ActivityMainBinding mActivityMainBinding ; 
    @Inject PresentationFragmentPresenter mView; 
    private Toolbar mToolbar; 


    @Override 
    protected void bindView(int layoutResource) { 
     mView = new PresentationFragmentPresenterImp(this); 
     mActivityMainBinding = DataBindingUtil.setContentView(this,layoutResource); 
    } 

    @Override 
    public int getLayoutResource() { 
     return R.layout.activity_main; 
    } 

我做错了吗?你知道Dagger和DataBinding之间是否存在不兼容?

回答

2

A组分具有提供dependendencies的方法有两种:

提供方法返回注入或提供的类型。

以下是提供方法的示例。请注意,它是一种简单返回依赖项的方法,在本例中为OKHttpClient

OkHttpClient httpClient(); 

成员注入方法即依赖注入特定类型。

下面是一个成员注入方法的示例。请注意,它需要一个参数,它是将它的依赖注入(类型在这种情况下MainActivity

void inject(MainActivity activity); 

你可以在Dagger 2 @Component javadocs阅读更多关于组件

你的问题。那void injectPresentationFragmentPresenter();既不是这些,也不返回任何东西,所以它不是一个提供方法,也没有参数,所以它不能成为成员注入方法,根据方法的命名和你的帖子的其余部分,我猜你想要的是定义这样的方法:

void injectPresentationFragmentPresenter(PresentationFragmentPresenter presenter);