Archives
Recent Posts
«   2024/07   »
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
관리 메뉴

안드로이드 개발자의 창고

[코틀린] UninitializedPropertyAccessException: lateinit property binding has not been initialized 본문

오류 해결

[코틀린] UninitializedPropertyAccessException: lateinit property binding has not been initialized

Wise-99 2023. 5. 25. 22:41

UninitializedPropertyAccessException: lateinit property binding has not been initialized

 

lateinit을 사용했지만 초기화하지 않아 발생하는 오류이다.

 

 

내 코드

class AllFragment : Fragment() {
    lateinit var binding: FragmentAllBinding
    
    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?
    ): View? {
        // 사용할 레이아웃, 인수(부모), 프레그먼트 자동으로 추가할 것 인지
        var binding = FragmentAllBinding.inflate(inflater, container, false)
        return binding.root
    }
    
    ...
    
}

내 경우에는 뷰바인딩을 위해 lateinit으로 binding이라는 변수를 선언해놨는데

View?{ } 에서 binding이라는 새로운 변수를 선언한 꼴이 됐다,,,

 

View?{ } 안에 있는 binding 옆에 var을 지워 해결했다!