package com.example.musicapp.Interface import TopBar import…
- User
package com.example.musicapp.Interface
import TopBar import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import com.example.musicapp.Data.Song import com.example.musicapp.ViewModel.AlbumDetailsViewModel import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.ui.Alignment import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.navigation.NavController import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource import coil.compose.rememberImagePainter import coil.transform.CircleCropTransformation import com.example.musicapp.Data.AlbumDetails import com.example.musicapp.ViewModel.PlayerViewModel import com.example.musicapp.R
@Composable fun AlbumDetailScreen(albumId: Int, navController: NavController) { val albumDetailsViewModel: AlbumDetailsViewModel = viewModel() val playerViewModel = hiltViewModel() albumDetailsViewModel.fetchAlbumDetails(albumId) val albumDetails by albumDetailsViewModel.albumDetails.collectAsState()
codealbumDetails?.let { details -> Scaffold( topBar = { TopBar(title = details.title) }, content = { padding -> Column( modifier = Modifier.padding(padding) ) { LazyColumn { itemsIndexed(details.songs) { _, song -> // Use itemsIndexed instead of items AlbumDetailItem(song, navController, playerViewModel) } } } } ) }}
@Composable fun AlbumDetailItem(song: Song, navController: NavController, playerViewModel: PlayerViewModel) { val context = LocalContext.current
code - Assistant
The problem here is that you're trying to use the
hiltViewModelfunction in a non-Hilt enabled Composable function. ThehiltViewModelfunction is resolved from theandroidx.hilt.navigation.composepackage, which requires Hilt Navigation Compose library. To fix this issue, follow these steps:- Add the Hilt Navigation Compose library to your
build.gradle(app level) file.
groovydependencies { // other dependencies implementation 'com.google.dagger:hilt-navigation-compose:1.0.0-alpha03' }- In your Composable function, import
hiltViewModel:
kotlinimport androidx.hilt.navigation.compose.hiltViewModelNow the
hiltViewModelfunction should resolve correctly, and the error should be gone. Remember to sync your project after adding the library to thebuild.gradlefile. - Add the Hilt Navigation Compose library to your
- User
- Assistant
- User
Unresolved reference: hilt:35,Unresolved reference: hiltViewModel:42 still have these problems
- Assistant
I apologize for the confusion.
Based on the updated information, it appears to be an issue with your Android Studio. To resolve the issue, follow these steps:
- In the Android Studio menu, click
File>Invalidate Caches / Restart. - Click on
Invalidate and Restartoption. - Wait for Android Studio to restart and build or load the project again.
After rebuilding, the unresolved references error should be resolved. If the problem persists, consider updating your Android Studio version to the latest stable version, as outdated IDE versions can sometimes cause errors when working with the latest libraries.
- In the Android Studio menu, click
- User
- Assistant
Apologies for the confusion. Based on your provided code, you are not using Hilt for dependency injection, since I don't see any Hilt annotations being used. In this case, you should use the
viewModelfunction instead ofhiltViewModelto get an instance of thePlayerViewModel.You can simply modify the
AlbumDetailScreenfunction as follows:kotlin@Composable fun AlbumDetailScreen(albumId: Int, navController: NavController) { val albumDetailsViewModel: AlbumDetailsViewModel = viewModel() // Replace hiltViewModel with viewModel val playerViewModel = viewModel<PlayerViewModel>() albumDetailsViewModel.fetchAlbumDetails(albumId) ... }This change should resolve the error. Make sure that you have the required ViewModel implementations and that they are set up correctly.