Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 540 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'by viewModels()' Kotlin property delegate unresolved reference

#1
I'm trying to implement viewmodel with kotlin. First I added the needed dependecies:

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0"

Then I created a simple viewmodel:

class MyViewModel: ViewModel() {
val greeting = "Hello World"
}

But when I tried to access the view model from the activity with kotlin property delegate:

val model by viewModels<MyViewModel>()

The compiler does not resolve `viewModels`. I don't know what the problem is. Did I miss something?
Reply

#2
For me the solution above this not work.

I needed to import :

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
Reply

#3
Add these dependencies:

implementation "androidx.activity:activity-ktx:$activity_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"

You can find the latest versions of libraries here:

[To see links please register here]


[To see links please register here]

Reply

#4
For me the solution above this not work.

Add this dependency: :

implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

Add This dependency :

implementation "androidx.lifecycle:lifecycleviewmodel:2.2.0"
Reply

#5
**Resolved this error**, using below dependency in module-level **build.gradle**

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

**OR**

implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.2.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.fragment:fragment-ktx:1.3.6'

I have also added below dependency to implement **ViewModel** in **Kotlin**

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'


My module-level **build.gradle** start as per below

plugins {
id 'com.android.application'
id 'kotlin-android'
}
Reply

#6
In my case, I was just using the wrong type of Activity. I had an `android.app.Activity` instead of an `androidx.appcompat.app.AppCompatActivity`.`viewModels()` is only available in the latter.
Reply

#7
For me, I was having the following dependencies:
```
implementation 'androidx.activity:activity-compose:1.5.0'
```
but still I faced this same error.


The reason was that `by viewModels()` must be called inside the `onCreate()`:
```
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val customViewModel: MyViewModel by viewModels()

setContent {
AppTheme {
Surface() {
MyApp(customViewModel = customViewModel)
}
}
}
}
}
```
and not in any other composable function. But I was trying to calling it from `MyApp()` composable function. Hence the error.
Reply

#8
It depends, on how you are using ViewModel. If you don't use fragments, this is enough to resolve "Unresolved reference: viewModels" for activity.
```
// ViewModel
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.activity:activity-ktx:1.6.1'
```
```
// is to add the dependency on Fragment
implementation 'androidx.fragment:fragment-ktx:1.2.5'
```

```
// is to add the dependency on Activity
implementation 'androidx.activity:activity-ktx:1.1.0'
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through