[Android] lateinit property has not been initialized μ—λŸ¬

μ•Œλ¦°Β·2024λ…„ 2μ›” 27일
0

TroubleShooting

λͺ©λ‘ 보기
24/25

μ—λŸ¬ 상황

πŸ‘‰ Fragment not attached to a context μ—λŸ¬ ν•΄κ²°
μœ„μ˜ μ—λŸ¬λ₯Ό ν•΄κ²°ν•˜κ³  λ‚˜λ‹ˆ λ‹€μŒκ³Ό 같은 μ—λŸ¬κ°€ λ‹€μ‹œ λ°œμƒν–ˆλ‹€.

kotlin.uninitializedpropertyaccessexception: lateinit property mActivity has not been initialized

μ—λŸ¬ 원인 및 ν•΄κ²°

μ—λŸ¬ 원인

lateinit 속성 mActivityκ°€ μ΄ˆκΈ°ν™”λ˜μ§€ μ•Šμ•„ λ°œμƒν•œ μ—λŸ¬μ΄λ‹€.

지연 μ΄ˆκΈ°ν™” 속성인 mActivityλ₯Ό onAttach()μ—μ„œ μ΄ˆκΈ°ν™” ν•˜μ˜€μ§€λ§Œ,
μ΄ˆκΈ°ν™” 이전에 λ‹€μŒ μ½”λ“œμ—μ„œ μ‚¬μš©λ˜μ–΄λ²„λ ΈκΈ° λ•Œλ¬Έμ— λ°œμƒν•œ μ—λŸ¬μ΄λ‹€.

val locationProvider = LocationProvider(mActivity)

μ—λŸ¬ ν•΄κ²°

이와 같은 μ—λŸ¬μ˜ 경우 ν•΄κ²° 방법이 크게 두 가지가 μžˆλ‹€.

  1. 지연 μ΄ˆκΈ°ν™” 속성을 μ‚­μ œν•˜κ³  null둜 μ„ μ–Έ ν›„ μ‚¬μš©
var locationProvider: LocationProvider? = null

과거에 이 λ°©λ²•μœΌλ‘œ μ—λŸ¬λ₯Ό ν•΄κ²°ν•œ ν¬μŠ€νŒ…μ΄ μžˆλ‹€.
πŸ‘‰ lateinit property listener has not been initialized μ—λŸ¬ ν•΄κ²°

  1. 지연 μ΄ˆκΈ°ν™” 속성을 κ°–κ³  μžˆλŠ” λ³€μˆ˜ μ‚¬μš©μ„ onCreateView() μ΄ν›„λ‘œ μˆ˜μ •ν•˜κΈ°
    ν˜„μž¬ λ‚˜μ˜ μƒν™©μ˜ 경우 이 2번 방법이 μ˜³λ‹€κ³  생각해 이 λ°©λ²•μœΌλ‘œ λ‹€μŒκ³Ό 같이 μˆ˜μ •ν•˜μ˜€λ‹€.

λ‹€μŒμ€ μ—λŸ¬κ°€ λ°œμƒν•˜λŠ” λΆ€λΆ„μ˜ μ½”λ“œμ΄λ‹€.

    private lateinit var mActivity: MainActivity
    
    val locationProvider = LocationProvider(mActivity)
    val uLatitude = locationProvider.getLocationLatitude()
    val uLongtitude = locationProvider.getLocationLongitude()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as MainActivity
    }

지연 μ΄ˆκΈ°ν™” λ³€μˆ˜μΈ mActivityκ°€ val locationProvider = LocationProvider(mActivity)μ—μ„œ μ‚¬μš©λ˜μ–΄ μ—λŸ¬κ°€ λ°œμƒν•œ 것이닀.

λ”°λΌμ„œ locationProvider μ΄ˆκΈ°ν™” μ½”λ“œλ₯Ό onCreateView()λ‚΄μ—μ„œ μž‘μ—…μ΄ μΌμ–΄λ‚˜λŠ” checkPermission() ν•¨μˆ˜ λ‚΄λ‘œ 이동해도 λ‘œμ§μƒ λ¬Έμ œκ°€ μ—†λ‹€κ³  νŒλ‹¨ν•˜μ—¬, ν•΄λ‹Ή ν•¨μˆ˜λ‘œ 이동해주어 μ—λŸ¬λ₯Ό ν•΄κ²°ν–ˆλ‹€.

μˆ˜μ •ν•œ μ½”λ“œ

    private lateinit var mActivity: MainActivity

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as MainActivity
    }
    
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentHomeBinding.inflate(layoutInflater)

        // ν˜„μž¬ λ‘œκ·ΈμΈν•œ μ‚¬μš©μž κ°€μ Έμ˜€κΈ°
        user = Firebase.auth.currentUser
        Log.d("MyTag", "HomeFragment user info: ${user}")

        checkAllPermissions()

        return binding.root
    }

    // FINE, COARSE ν—ˆμš© 확인
    private fun checkPermission() {
        val finePermissionCheck =
            ContextCompat.checkSelfPermission(mActivity, ACCESS_FINE_LOCATION)
        val coarsePermissionCheck =
            ContextCompat.checkSelfPermission(mActivity, ACCESS_COARSE_LOCATION)

        if (finePermissionCheck != PackageManager.PERMISSION_GRANTED || coarsePermissionCheck != PackageManager.PERMISSION_GRANTED) {
            // κΆŒν•œμ΄ ν•œ κ°œλΌλ„ μ—†λ‹€λ©΄ νΌλ―Έμ…˜ μš”μ²­
            ActivityCompat.requestPermissions(
                mActivity,
                REQUIRED_PERMISSIONS,
                PERMISSIONS_REQUEST_CODE
            )
        } else {
        	val locationProvider = LocationProvider(mActivity)
    		val uLatitude = locationProvider.getLocationLatitude()
    		val uLongtitude = locationProvider.getLocationLongitude()
            
            Log.d("MyTag", "Latitude: $uLatitude, Longitude: $uLongtitude")

            startPosition = LatLng.from(uLatitude, uLongtitude)

            val mapView: MapView = binding.mapView
            mapView.start(lifeCycleCallback, readyCallback)
        }
    }
profile
Android 짱이 λ˜κ³ μ‹Άμ€ 개발 기둝 (+ ios도 μ‘°κΈˆμ”©,,πŸ‘©πŸ»β€πŸ’»)

0개의 λŒ“κΈ€