无法在Kotlin Dagger2 mvvm中注入接口

时间:2019-11-28 11:18:15

标签: android kotlin mvvm dagger-2 android-mvvm

嗨,当我尝试在下面的类中插入Doa接口时,我正在使用mvvm和dagger2

@Singleton
class PodCastRepository @Inject constructor(private val dao: PodCastDao) {
}

我的界面类

interface PodCastDao {

    fun getLegoThemes(): LiveData<PodCast>

}

我遇到以下错误

  

错误:[Dagger / MissingBinding]   无法提供in..myapplication.podcast.data.PodCastDao   没有@Provides注释的方法。公共抽象接口   AppComponent {                   ^         在.myapplication.podcast.data.PodCastDao中注入             in..myapplication.podcast.data.PodCastRepository(dao)         in..myapplication.podcast.data.PodCastRepository在以下位置注入             in..myapplication.podcast.PodCastViewModel(存储库)         in..myapplication.podcast.PodCastViewModel在             in..myapplication.di.ViewModelModule.bindThemeViewModel(viewModel)         java.util.Map,javax.inject.Provider>   在注入             in..myapplication.di.ViewModelFactory(creators)         in..myapplication.di.ViewModelFactory被注入             in..myapplication.di.ViewModelModule.bindViewModelFactory(factory)         androidx.lifecycle.ViewModelProvider.Factory被注入             in..myapplication.podcast.PodcastFragment.viewModelFactory         in..myapplication.podcast.PodcastFragment在以下位置注入             dagger.android.AndroidInjector.inject(T)[in..myapplication.di.AppComponent→   in..myapplication.di.MainActivityModule_ContributeMainActivity.MainActivitySubcomponent   →   in..myapplication.di.FragmentBuildersModule_ContributeThemeFragment.PodcastFragmentSubcomponent]

请帮助我解决此问题,我是匕首的新手,并且我将Kotlin与mvvm一起使用

2 个答案:

答案 0 :(得分:0)

Dagger不能只创建某个接口实例。这就是错误的意思。您需要告诉Dagger需要注入PodCastDao时提供什么。

因此,如果您还没有模块,则需要创建一个模块。然后,您需要在其中编写代码来告诉Dagger要注入PodCastDao的内容:

@Module
class DaoModule {

    // This will tell Dagger how to construct PodCastDao when it's injected somewhere.
    // Could be annotated with @Singleton
    @Provides
    fun providePodCastDao(): PodCastDao {
        return PodCastDaoImpl() // Create implementation for the interface
    }

    // Other Providers for XyzDaos ...

}

并且您需要将此模块添加到您的AppComponent:

@Singleton // Or not, depends what you want
@Component(modules = { DaoModule.class /*, other modules*/ })
interface AppComponent { }

答案 1 :(得分:0)

首先需要提供数据库。然后使用您提供的dao方法返回dao

例如:

private void Delete_Single_Line_Row(string docentry, int lNum)
        {
            SAPbobsCOM.Documents oSalesOrd = null;
            oSalesOrd = (SAPbobsCOM.Documents)SBOC_SAP.G_DI_Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders);
            int docEntry = Convert.ToInt32(docentry);
            int lineNum = lNum;

            // Load your sales orders
            if (oSalesOrd.GetByKey(docEntry))
            {
                // Go through your line items
                for (int i = 0; i < oSalesOrd.Lines.Count; i++)
                {
                    oSalesOrd.Lines.SetCurrentLine(i);
                    if (oSalesOrd.Lines.LineNum == lineNum) //Find your line number that you want delete.
                    {
                        oSalesOrd.Lines.Delete(); //Delete
                        break;
                    }
                }
                // Update your sales order
                int result = oSalesOrd.Update();

            }
        }    enter code here