InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.12k stars 721 forks source link

view model with fragment not working #474

Closed sunny0092 closed 5 years ago

sunny0092 commented 5 years ago

Describe the bug Please help me Below example, I have followed the instructions, because of something that it does not work.

To Reproduce

Application

import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

open class AppApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        configureDi()
    }

    // CONFIGURATION ---
    open fun configureDi() =
        startKoin{
            // Android context
            androidContext(this@AppApplication)
            // modules
            provideComponent()
        }

    // PUBLIC API ---
    open fun provideComponent() = appComponent
}

Use code like this in module:

val appComponent= listOf(
    remoteModule(Constants.API_NAME),
    featureModule,
    repositoryModule,
    coreModule)
----------------------
------------------------------------------
import org.koin.android.viewmodel.ext.koin.viewModel
import org.koin.dsl.module.module

val featureModule = module {
    factory { GetTopNewestUseCase(get()) }
    viewModel { HomeViewModel(get(), get()) }
}

Activity

import org.koin.android.viewmodel.ext.android.viewModel

    private lateinit var binding: FragmentHomeBinding
    private val viewModel: HomeViewModel by viewModel()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        binding.viewmodel = viewModel
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

Error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: net.ihaha.viblo, PID: 4697
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.ihaha.viblo/net.ihaha.viblo.home.HomeActivity}: org.koin.core.error.NoBeanDefFoundException: No definition found for 'net.ihaha.viblo.home.viewmodel.HomeViewModel' has been found. Check your module definitions.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5019)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for 'net.ihaha.viblo.home.viewmodel.HomeViewModel' has been found. Check your module definitions.
        at org.koin.core.scope.Scope.findDefinition(Scope.kt:170)
        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:164)
        at org.koin.core.scope.Scope.get(Scope.kt:128)
        at org.koin.android.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:63)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
        at org.koin.android.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:40)
        at org.koin.android.viewmodel.ViewModelResolutionKt.getViewModel(ViewModelResolution.kt:20)
        at org.koin.android.viewmodel.ext.android.LifecycleOwnerExtKt.getViewModel(LifecycleOwnerExt.kt:85)
        at net.ihaha.viblo.home.HomeFragment$$special$$inlined$viewModel$1.invoke(LifecycleOwnerExt.kt:95)
        at net.ihaha.viblo.home.HomeFragment$$special$$inlined$viewModel$1.invoke(LifecycleOwnerExt.kt)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at net.ihaha.viblo.home.HomeFragment.getViewModel(HomeFragment.kt)
        at net.ihaha.viblo.home.HomeFragment.onCreateView(HomeFragment.kt:24)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2544)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:884)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2092)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1822)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1723)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2624)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2580)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2571)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:907)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1235)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1301)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2620)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2580)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
        at android.app.Act

Koin project used and used version (please complete the following information): org.koin:koin-android-viewmodel:2.0.0

Thanks

arnaudgiuliani commented 5 years ago

Your forgot modules() section in your startKoin declaration.

Would be more modules(appComponent) in your case

PauloLinhares09 commented 5 years ago

val appModule = module { //Single instance from repository single{ AdaptersImpl() } single{ MainCardAdapter() } single { MainCardOptionsAdapter() } single { LinearSnapHelper() }

//fragments
single{ FragmentListAudiosSeqAdapter() }

//Repository
single { RepositoryLocal() }

//RX
single { CompositeDisposable() }

viewModel {} //<<<<<<<<<<<<<<< It not find

//Factory
factory { MainActivityPresenter(get(), get(), get(), get()) }
factory { ListAudiosSeqFragmentPresente(get(), get(), get(), get()) }

}

Why not works for me ? I'm using the last version koin