3

I am facing an issue while integrating Hilt with my view Model. The application gets crash right away after the launch. Any will be appreciated because I may be making a basic mistake.

//Here is my code for MainActivity:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NoteJetTheme {
                // A surface container using the 'background' color from the theme
//                val noteViewModel = viewModel<NoteViewModel>()
                val noteViewModel: NoteViewModel by viewModels()
                NoteApp(noteViewModel = noteViewModel)
            }
        }
    }
}

//and here's the viewModel code:

@HiltViewModel
class NoteViewModel @Inject constructor(private val repo : NoteRepo): ViewModel() {
var \_noteList = MutableStateFlow\<List\<UserNote\>\>(emptyList())
var noteList = \_noteList.asStateFlow()

    init {
      //  noteList.addAll(NoteDataSource().loadNotes())
        viewModelScope.launch(Dispatchers.IO) {
            repo.getAllNotes().distinctUntilChanged()
                .collect{ listOfNote ->
                    if(listOfNote.isNotEmpty())
                        _noteList.value = listOfNote
                    else
                        Log.d("noteListFlow", "list is empty")
                }
        }
    }

These are the libraries that I used in this project.

// here are the libs
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"

    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"

    implementation "androidx.room:room-ktx:$room_version"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • I also saw this question. https://stackoverflow.com/questions/73302605/creationextras-must-have-a-value-by-saved-state-registry-owner-key but his issue was related to navigation ,I am not even using the navigation – Zaki Siddiqui Feb 20 '23 at 16:41
  • Please include the `dependencies` block from your build.gradle as these APIs require a [minimum version of the AndroidX Activity library](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.5.0) – ianhanniballake Feb 20 '23 at 16:52
  • I have added the project libs as well – Zaki Siddiqui Feb 20 '23 at 17:05

2 Answers2

0

I had exactly the same issue but in my case the viewmodel was not getting created in Fragment. Adding dependency with Fragment-ktx helped resolve this issue. Here's the solution I found - https://stackoverflow.com/a/56853282/7521936

-1

As per the Lifecycle 2.5.0 release notes:

ViewModel CreationExtras - These extras are provided automatically by your Activity or Fragment when using Activity 1.5.0 and Fragment 1.5.0, respectively.

And looking at those Activity 1.5.0 release notes:

CreationExtras Integration - ComponentActivity now has the ability to provide a stateless ViewModelProvider.Factory via Lifecycle 2.5.0’s CreationExtras.

Since you are using ComponentActivity, you need to upgrade to Activity 1.5.0 or higher in order to use libraries like Hilt that require the new CreationExtras APIs.

// Use the latest stable version of activity-compose
// but any version 1.5.0 or higher will work
implementation 'androidx.activity:activity-compose:1.6.1'
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thankyou so much @ianhanniballake. Your answer is right. but I also found a way around to solve this problem. It turns out issue was in the way I was initializing the viewModel `val noteViewModel: NoteViewModel by viewModels()`. As soon as I switched it to `val noteViewModel = viewModel()`, my problem get resolved but I have also verified your answer and it is correct. Thanks again for your help – Zaki Siddiqui Feb 27 '23 at 14:47
  • @ZakiSiddiqui Yes you are right. I got the issue fixed as per your suggestion. Thanks – Deepak Rattan Apr 13 '23 at 15:18
  • glad I can Help :) – Zaki Siddiqui May 25 '23 at 20:52