inflearn logo
강의

Course

Instructor

Modern Android - Getting Started with Jetpack Compose

State Deepening

viewModel() 오류 추가질문입니다

1118

shafeel2

67 asked

0

dependencies {

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.2")
    implementation("androidx.activity:activity-compose:1.8.0")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}

위와 같이 build.gradle.kts 파일내용을 변경한 후

아래아 같이 코드를 작성하면 34번줄 viewModel() 에서 오류가 발생합니다

q1.png

또 viewModel() 에서 Alt+Enter 키를 누르면 아래처럼

여러개의 항목을 선택하는 창이 뜨는데 이중 어느것을 선택해야 하는지 ...

q2.png

android kotlin jetpack

Answer 2

0

shafeel2

lifecycle_version 이 2.5.1 이면 오류가 사라지나

2.6.2 로 하였을때는

import androidx.lifecycle.viewmodel.compose.viewModel

가 되지 않았습니다

그래서 build.gradle 파일내용을 아래와 같이 수정하여 실행하니 오류가 사라졌습니다

dependencies {
    val lifecycle_version = "2.5.1"
    val arch_version = "2.1.0"

    // ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
    // ViewModel utilities for Compose
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
    // LiveData
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
    // Lifecycles only (without ViewModel or LiveData)
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")

    // Saved state module for ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")

    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version")

    // optional - helpers for implementing LifecycleOwner in a Service
    implementation("androidx.lifecycle:lifecycle-service:$lifecycle_version")

    // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
    implementation("androidx.lifecycle:lifecycle-process:$lifecycle_version")

    // optional - ReactiveStreams support for LiveData
    implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version")

    // optional - Test helpers for LiveData
    testImplementation("androidx.arch.core:core-testing:$arch_version")

    // optional - Test helpers for Lifecycle runtime
    testImplementation ("androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version")

.....

 

수고많으신 강사님

늘 건강하시고 부자되세요

 

0

shafeel2

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            HomeScreen()
        }
    }
}

@Composable
fun HomeScreen(viewModel : MainViewModel = viewModel()) {
    Column() {
        Text("Hello World")
        Button(onClick = {  }) {
            Text("Click !")
        }
    }
}

class MainViewModel: ViewModel() {
    private val _value = mutableStateOf("Hello World")
    val value: State<String> = _value
}

1

survivalcoding

보내주신 코드를 그대로 붙여서 해 보니 정말로 Alt + Enter 눌렀을 때 import 옵션이 안 나오는 현상을 발견했습니다!! 버그 인 것 같습니다.

수동으로 import 를 추가해 주시면 될 것 같습니다.

import androidx.lifecycle.viewmodel.compose.viewModel

버그 때문에 고생하셨습니다.

ViewModel 사용 관련 질문

0

75

1

onTabFavorite 콜백 관련 질문

0

60

2

livedata가 왜 필요한 건지 궁금합니다

0

128

3

깃허브에 있는 MemoryTodoRepository 는 룸을 사용하는게 아닌 메모리에 저장, 수정, 삭제 하는건가요?

0

172

1

이젠 아래와 같은 오류가 뜨는데 KSP가 문제 인걸까요?

0

369

2

영상 1분쯤에서 MainActivity에서 viewModel이 저는 안되고 그래들 문제인거 같은데 해결 방법을 모르겠습니다.

0

186

1

전자액자에서 영상과 깃허브의 내용이 달라서 영상을 다보고 깃허브 내용으로 돌려봤는데 권한요청부터가 안됩니다.

0

189

2

Navigation수업에서 string대신 bitmap을 인자로 넘겨주는 방법?

0

177

1

TodoList - 04에서 recentlyDeleteTodo가 null일 경우 처리 방법 문의

0

171

1

나만의 웹 브라우저 03 - UI와 ViewModel 연동 강의에서 질문들이 있습니다.

0

205

2

Scaffold를 사용하면 Content padding parameter it is not used 에러

0

350

1

Material3로 바뀌면서 강의랑 다른 부분이 초반부터 있는데요.

0

221

1

강의에서 사용하는 리소스(이미지 등)을 다운 받을 수 있으면 좋겠어요.

0

248

2

Card Compose에서 elevation 옵션

1

347

1

구글맵 질문입니다

0

233

1

구글맵강의중에서 ..

0

578

7

drawCircle( color = Color.... 부분에서 빨간줄이 ...

0

269

2

val scaffoldState = rememberScaffoldState() 에서 빨간줄이 생기네요

0

524

1

나만의 웹브라우져 02 코드실행시 에뮬레이터에 따라

0

221

1

나만의 웹브라우져 01 코딩 후 실행하면 아래와 같은 오류가 뜨네요

0

3221

2

비만도계산기 로직작성 에서 문제가 발생했어요 ^^

0

338

3

viewModel: MainViewModel = viewModel() 에 오류있습니다

0

537

3

AAR metadata 관련오류발생

0

1437

3

모바일화면에 키보드가 사라지지 않아

0

250

1