Archives
Recent Posts
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Today
Total
관리 메뉴

안드로이드 개발자의 창고

[코틀린] Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable. 전역변수 오류 본문

오류 해결

[코틀린] Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable. 전역변수 오류

Wise-99 2023. 5. 7. 01:14

 

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable.

 

노란색 밑줄이라서 심각한 오류는 아니고 변수명 변경 같은 권고사항인줄 알고 넘겼는데 오류가 났다.

오류를 자세히 보니 저 라인을 가리키고 있었다.

 

메뉴 버튼을 클릭하면 왼쪽에서 메뉴가 나오게끔 하기 위해 DrawerLayout를 사용했다.

하고나서보니 onCreate()의 setOnClickListener에서도 불러오고 onNavigationItemSelected()에서도 불러와야 했다.

 

난 그저 xml에 있는 DrawerLayout의 id를 너무 많이 선언하는 것 같아서 밖으로 빼버린건데..

이건 내 영어실력 밖의 문장이라 파파고를 데려왔다...

 

 

 

 

파파고가 해석해준 것과 코틀린 문법을 생각해보면 null 관련 문제인 것 같았다.

애초에 View 자체가 생성되지 않았는데 id를 가져오려고 하니 오류가 나는 거였다...

그냥 무지성 전역변수 선언..

그렇다고 onCreate()에서만 선언해주자니 밑에 override fun onNavigationItemSelected() 에서 오류가 났다.

 

 

이것 저것 뒤져보다가 해결방법을 찾았다.

lateinit을 사용해서 해결했다.

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    lateinit var layoutDrawer: DrawerLayout

    ...
}
 

 

 

 

이걸 사용하여 전역변수로 선언해줬고, 밑에 onCreate() 부분에

layoutDrawer = findViewById<DrawerLayout>(R.id.layout_drawer)
 

를 선언하니 밑에 onNavigationItemSelected()에서 오류가 나타나지 않았다!