Java 没有构造函数,无法提供匕首2
对不起,我的英语,现在我开始学习匕首2,我不明白为什么我有错误: 错误:(9,10)错误: test.dagger.dagger.modules.MainActivityPresenterModule不能为空 不使用@Inject构造函数或从@Provides提供-或 @生成带注释的方法。 test.dagger.dagger.modules.main活动当前模块在以下位置注入 test.dagger.view.activitys.MainActivity.mainActivityPresenterModule test.dagger.view.activitys.main活动在 测试.dagger.dagger.components.AppComponent.injectMainActivity(mainActivity) 应用程序Java 没有构造函数,无法提供匕首2,java,android,dagger-2,Java,Android,Dagger 2,对不起,我的英语,现在我开始学习匕首2,我不明白为什么我有错误: 错误:(9,10)错误: test.dagger.dagger.modules.MainActivityPresenterModule不能为空 不使用@Inject构造函数或从@Provides提供-或 @生成带注释的方法。 test.dagger.dagger.modules.main活动当前模块在以下位置注入 test.dagger.view.activitys.MainActivity.mainActivityPresent
public class App extends Application {
private static AppComponent component;
@Override
public void onCreate() {
super.onCreate();
component = DaggerAppComponent.create();
}
public static AppComponent getComponent() {
return component;
}
}
AppComponent
@Component(modules = {MainActivityPresenterModule.class})
public interface AppComponent {
void injectMainActivity(MainActivity mainActivity);
}
MainActivityPresenterModule
@Module
public class MainActivityPresenterModule {
@Provides
MainActivityPresenter provideActivityPresenter(NetworkUtils networkUtils) {
return new MainActivityPresenter(networkUtils);
}
@Provides
NetworkUtils provideNetworkUtils(){
return new NetworkUtils();
}
}
NetworkUtils
public class NetworkUtils {
public boolean isConnection() {
return true;
}
}
MainActivityPresenter
public class MainActivityPresenter {
NetworkUtils networkUtils;
public MainActivityPresenter(NetworkUtils networkUtils) {
this.networkUtils = networkUtils;
}
public void getUser(){
if(networkUtils.isConnection()) {
Log.e("getUser", "getUser");
} else {
Log.e("no internet", "no internet connection");
}
}
}
main活动
public class MainActivity extends AppCompatActivity {
@Inject
MainActivityPresenterModule mainActivityPresenterModule;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App.getComponent().injectMainActivity(MainActivity.this);
}
}
您只能注入用
@Module
注释的类中提供的内容(仅该模块内用@注释的方法提供的内容)。因此,例如,您可以执行@injectmainActivityPresenter
,而不是像您尝试的那样尝试注入整个模块。模块应在Dagger初始化时注册,如下所示(在应用程序#onCreate中)
在MainActivity中,您只需调用inject即可注入您的@inject MainActivityPresenter演示者
或模块中定义的任何其他注入,如下所示:
@Inject MainActivityPresenter presenter
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
(application as App).component.inject(this)
// after #inject(this) above you can start using your injections:
presenter.getUser()
}
抱歉,我在Kotlin中编写了代码片段,因为这样编写要少得多,希望您能了解它在Java中的外观。您只能注入用@Module
注释的类中提供的内容(只有在该模块中用@注释的方法提供了)。因此,例如,您可以执行@injectmainActivityPresenter
,而不是像您尝试的那样尝试注入整个模块。模块应在Dagger初始化时注册,如下所示(在应用程序#onCreate中)
在MainActivity中,您只需调用inject即可注入您的@inject MainActivityPresenter演示者
或模块中定义的任何其他注入,如下所示:
@Inject MainActivityPresenter presenter
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
(application as App).component.inject(this)
// after #inject(this) above you can start using your injections:
presenter.getUser()
}
很抱歉,我用Kotlin编写了代码片段,因为用这种方式编写代码要少得多,希望您了解它在Java中的外观