Android Dagger 2无法解析符号构建器()

时间:2016-10-21 12:39:01

标签: android dagger-2 dagger

我在Android Studio中使用Dagger 2,但是当尝试通过 DaggerMyComponent.builder()。myModule(new MyModule())。build()创建Component时,我总是看到单词" builder()"用红色表示"无法解析符号构建器"。

我必须说,我已经多次清理并构建项目,甚至评论组件实例并在重建后对其进行解除注释。 我还必须说我可以检查DaggerMyComponent.class,一切似乎都没问题,事实上,我可以毫无问题地导入该类。

该示例在GithHub上共享,链接:

https://github.com/alessandroargentieri/LetsDagger2Together

我有两个类:BClass和AClass,它取决于第一个。 我有ABModule.class,它是工厂方法,ABComponent是组件接口。 DaggerABComponent创建到WholeApplication.class中,扩展了Application。

package mawashi.alex.letsdagger2together.Application;

import android.app.Application;
import mawashi.alex.letsdagger2together.DaggerClasses.ABComponent;
import mawashi.alex.letsdagger2together.DaggerClasses.ABModule;
import mawashi.alex.letsdagger2together.DaggerClasses.DaggerABComponent;

public class WholeApplication extends Application {
    static ABComponent component;

    @Override
    public void onCreate() {
        super.onCreate();
        //here is where builder() is not recognized
        component = new DaggerABComponent.builder().aBModule(new ABModule()).build();

    }

    public static ABComponent getComponent(){
        return component;
    }
}

模块:

package mawashi.alex.letsdagger2together.DaggerClasses;

import dagger.Module;
import dagger.Provides;
import mawashi.alex.letsdagger2together.Model.AClass;
import mawashi.alex.letsdagger2together.Model.BClass;

@Module
public class ABModule {

    @Provides
    public BClass provideBClass(){
       return new BClass("xxx");
    }

    @Provides
    public AClass provideAClass(BClass bClass){
       return new AClass(bClass);
    }
}

组件:

package mawashi.alex.letsdagger2together.DaggerClasses;

import dagger.Component;
import mawashi.alex.letsdagger2together.MainActivity;

@Component (modules = {ABModule.class})
public interface ABComponent {
    public void inject(MainActivity mainActivity);
}

要注入的类:

package mawashi.alex.letsdagger2together.Model;

public class BClass {
    private String xfactor;
    public BClass(String xfactor){
        this.xfactor = xfactor;
    }
    public String getXfactor(){
        return xfactor;
    }
}


package mawashi.alex.letsdagger2together.Model;

public class AClass {
    private BClass b;
    public AClass(BClass b){
        this.b = b;
    }
    public String getYfactor(){
        return "Y-" + b.getXfactor() + "-Y";
    }
}

放置注入AClass和BClass的位置:

package mawashi.alex.letsdagger2together;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import javax.inject.Inject;

import mawashi.alex.letsdagger2together.Application.WholeApplication;
import mawashi.alex.letsdagger2together.DaggerClasses.ABModule;
import mawashi.alex.letsdagger2together.DaggerClasses.DaggerABComponent;
import mawashi.alex.letsdagger2together.Model.AClass;

public class MainActivity extends AppCompatActivity {

    @Inject AClass a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WholeApplication.getComponent().inject(this);

        Toast.makeText(this, a.getYfactor(),Toast.LENGTH_LONG).show();
    }
}

如果有人知道我的错误,那对我来说会很好。感谢大家。

2 个答案:

答案 0 :(得分:8)

哦,我已经弄清楚了。

你添加了这一行:

    component = new DaggerABComponent.builder().aBModule(new ABModule()).build();

它应该是这一行:

    component = DaggerABComponent.builder().aBModule(new ABModule()).build();

所以只需删除new,然后就可以了。

答案 1 :(得分:-1)

@Inject注释的所有字段都必须是公共的: 您的活动中的@Inject public AClass a;