-
[이력서 꾸미기] Android Studio viewModel() 에러개발/프로젝트2(유데미) 2024. 7. 1. 19:32
요즘 Kotlin을 하고 있는데 에러가 안나면 공부가 아니지..^^MVVM을 하는 중에
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val viewModel : CounterViewModel = viewModel() CounterMVVMTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { TheCounterApp(viewModel) } } } } }
이렇게 MainActivity 부분에서 viewModel() 이부분만 자꾸 에러가 나는 것이었다
그래서 저 부분을 빼고 바로 TheCounterApp(CounterViewModel())
이렇게 적었더니 에러는 나지 않지만 화면을 돌렸을때 Count가 0으로 돌아가고 있었다
그거때문에 이걸 하는건데
그래서 구글링해봤다
우선 build.gradle.kts 파일로 가서
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1")
이렇게 붙여 넣고 다시 MainActivity 파일로 돌아와서
import androidx.lifecycle.viewmodel.compose.viewModel
임포트 하면 viewModel()부분이 초록색으로 변한다
viewModel() 함수에 에러가 나는 이유는 종속성이 추가되지 않아서 발생하는 문제라고 한다
그래서 androidx.lifecycle:lifecycle-viewmodel-compose 라이브러리를 추가하고
ViewModel의 상태가 화면 회전 등으로 인해 초기화되지 않고 유지될 수 있다고 한다
'개발 > 프로젝트2(유데미)' 카테고리의 다른 글
[이력서 꾸미기] React 완벽 가이드 코딩연습1 (0) 2023.07.13 [이력서 꾸미기] flyio로 배포하기 (1) 2023.05.25 [이력서 꾸미기] 마지막을 향해가는 이력서 꾸미기 (0) 2023.05.22 [이력서 꾸미기] Geocode 오류때문에 얼마나 시간을 허비했는지 (0) 2023.05.02 [이력서 꾸미기] Mongoose 에러 해결하기 (0) 2023.03.15