使用Dagger 2注入Androidx片段 [英] Injecting Androidx Fragments using Dagger 2
本文介绍了使用Dagger 2注入Androidx片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用匕首2注入我的Androidx片段。在我的活动中,我有:
I want to inject my Androidx fragments using dagger 2. In my activity I have:
public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector
{
@Inject Reposetory reposetory;
@Inject DispatchingAndroidInjector<androidx.fragment.app.Fragment> dispatchingAndroidInjector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public AndroidInjector<androidx.fragment.app.Fragment> supportFragmentInjector()
{
return dispatchingAndroidInjector;
}
}
问题是当我想要构建项目时出现此错误:
the problem is when i want to build the project i get this error:
error: cannot find symbol class MapBuilder
并且在DispatchingAndroidInjector中将 androidx.fragment.app.Fragment 更改为 Fragment 时,我没有收到此错误
and when i change androidx.fragment.app.Fragment to Fragment in DispatchingAndroidInjector i don't get this error any more.
推荐答案
Androidx尚不支持,但是启用jetifier可能会解决您的问题。
Androidx is not supported yet, but enabling jetifier may solve your problem.
只需将以下代码添加到您的 gradle.properties
Just add the below code to your gradle.properties
android.useAndroidX=true
android.enableJetifier=true
另请参阅以下问题:
AndroidInjection对androidx Fragment的支持
这篇关于使用Dagger 2注入Androidx片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文