草庐IT

java - 如果没有 @Provides- 或 @Produces-annotated 方法,则无法提供 Dagger2

coder 2023-12-11 原文

所以我正在开发一个具有 mvp 模式的应用程序,但我在使用 Dagger2 时遇到了问题

我为演示者创建了一个名为 HelloPresenter 的界面。

然后我为该接口(interface)创建了一个名为 HelloPresenterImpl 的实现

我的 HelloPresenter

public interface HelloPresenter {

    public void sayHello();

}

我的 HelloPresenterImpl

public class HelloPresenterImpl implements HelloPresenter {

    StoryView storyView;

    @Inject
    public HelloPresenterImpl(StoryView storyView) {
        this.storyView = storyView;
    }

    @Override
    public void sayHello() {
        Log.d(TAG, "hello there");
    }

}

我的组件

@UserScope
@Component(dependencies = NetComponent.class, modules = RetrofitModule.class)
public interface StoryComponent {
    void inject(MainActivity activity);
    void inject(HelloPresenter helloPresenter);
}

我的模块

@Module
public class HelloModule {

    @Provides
    public HelloPresenter providesHelloPresenter(final HelloPresenterImpl presenter){
        return presenter;
    }

}

我的基础应用

public class BaseApplication extends Application {

    private NetComponent mNetComponent;
    private StoryComponent mStoryComponent;

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

        mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this))
                .netModule(new NetModule())
                .build();

        mStoryComponent = DaggerStoryComponent.builder()
                .netComponent(mNetComponent)
                .retrofitModule(new RetrofitModule())
                .storyModule(new StoryModule())
                .build();

    }

    public NetComponent getNetComponent() {
        return mNetComponent;
    }

    public StoryComponent getStoryComponent() {
        return mStoryComponent;
    }

现在,当我尝试将它注入(inject)到我的 MainActivity 中时,它会抛出一个错误提示

Error:(18, 10) error: com.exampleapp.Hello.presenter.HelloPresenter cannot be provided without an @Provides- or @Produces-annotated method. com.exampleapp.Hello.presenter.HelloPresenter is injected at com.exampleapp.Hello.MainActivity.HelloPresenter com.exampleapp.Hello.MainActivity is injected at com.exampleapp.di.components.HelloComponent.inject(activity)

public class MainActivity extends AppCompatActivity {

    @Inject
    SharedPreferences mSharedPreferences;

    @Inject
    StoryInterface storyInterface;

    @Inject 
    HelloPresenter helloPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((BaseApplication) getApplication()).getStoryComponent().inject(this);

        helloPresenter.sayHello();  // This throws an Error

    }

请有人能指出我正确的方向吗?谢谢

最佳答案

没有看到整个项目,我的猜测是 Dagger 不知道如何创建 HelloPresenterImpl 实例,因为您没有提供 StoryView。我认为您需要:

@Provides
public StoryView providesStoryView() {
    return new StoryView();
}

关于java - 如果没有 @Provides- 或 @Produces-annotated 方法,则无法提供 Dagger2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754957/

有关java - 如果没有 @Provides- 或 @Produces-annotated 方法,则无法提供 Dagger2的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  7. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  8. ruby-on-rails - 如果我将 ruby​​ 版本 2.5.1 与 rails 版本 2.3.18 一起使用会怎样? - 2

    如果我使用ruby​​版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby​​1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更

  9. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  10. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

随机推荐