Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference failed while attempting to inject ViewModel #46

Closed
caleb-allen opened this issue Feb 6, 2018 · 4 comments
Closed

Type inference failed while attempting to inject ViewModel #46

caleb-allen opened this issue Feb 6, 2018 · 4 comments

Comments

@caleb-allen
Copy link

I believe I have followed the docs correctly, but I can't seem to get this working.

I have an activity:

class LoginActivity : BaseActivity() {
    val viewModel: LoginViewModel by getViewModel()
. . .

And I am instantiating my module in my Application class:

val myModule : Module = applicationContext {
            . . . .
            viewModel { MyViewModel(get()) }
}
startKoin(this, listOf(myModule))

but I'm getting a compile error on the by getViewModel() line:

Type inference failed: Not enough information to infer parameter T in intline fun <reified T : ViewModel> FragmentActivity.getViewModel(): T
Please specify it explicitly

If I attempt to specify it via getViewModel<MyViewModel>(), I get this compile error:

Missing 'getValue(MyActivity, KPropery<*>)' method on delegate of type 'MyViewModel'

Or if I try:

val viewModel = getViewModel<LoginViewModel>()

I get a runtime NPE when the ViewModel can't get any instance correctly:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Application.registerActivityLifecycleCallbacks(android.app.Application$ActivityLifecycleCallbacks)' on a null object reference
                                                       at android.arch.lifecycle.HolderFragment$HolderFragmentManager.holderFragmentFor(HolderFragment.java:169)
                                                       at android.arch.lifecycle.HolderFragment.holderFragmentFor(HolderFragment.java:84)
                                                       at android.arch.lifecycle.ViewModelStores.of(ViewModelStores.java:47)
                                                       at example.MyActivity.<init>(LoginActivity.kt:45)
                                                       at java.lang.Class.newInstance(Native Method)
                                                       at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)

Any insights would be greatly appreciated!

@arnaudgiuliani
Copy link
Member

Hello,

the delegate form of getViewModel() is by viewModel()

Replace your declaration in your LoginActivity with :
val viewModel: LoginViewModel by viewModel()

If you want to write the val viewModel = getViewModel<LoginViewModel>(), you must use it in the onCreate function.

Tell me if it fixes your situation.

@fredy-mederos
Copy link
Contributor

In the README file there are a little mistake.

// Bind it to your Activity
class MyActivity() : AppCompatActivity() {

  val viewModel : MyViewModel by getViewModel()

  override fun onCreate(){
    super.onCreate()
  }
}

Should be by viewModel()

@arnaudgiuliani
Copy link
Member

Just fixed it. Thanks !

@caleb-allen
Copy link
Author

This was the correct solution, using by viewModel() rather than by getViewModel(). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants