π Fragment not attached to a context μλ¬ ν΄κ²°
μμ μλ¬λ₯Ό ν΄κ²°νκ³ λλ λ€μκ³Ό κ°μ μλ¬κ° λ€μ λ°μνλ€.
kotlin.uninitializedpropertyaccessexception: lateinit property mActivity has not been initialized
lateinit μμ± mActivityκ° μ΄κΈ°νλμ§ μμ λ°μν μλ¬μ΄λ€.
μ§μ° μ΄κΈ°ν μμ±μΈ mActivityλ₯Ό onAttach()μμ μ΄κΈ°ν νμμ§λ§,
μ΄κΈ°ν μ΄μ μ λ€μ μ½λμμ μ¬μ©λμ΄λ²λ ΈκΈ° λλ¬Έμ λ°μν μλ¬μ΄λ€.
val locationProvider = LocationProvider(mActivity)
μ΄μ κ°μ μλ¬μ κ²½μ° ν΄κ²° λ°©λ²μ΄ ν¬κ² λ κ°μ§κ° μλ€.
var locationProvider: LocationProvider? = null
κ³Όκ±°μ μ΄ λ°©λ²μΌλ‘ μλ¬λ₯Ό ν΄κ²°ν ν¬μ€ν
μ΄ μλ€.
π lateinit property listener has not been initialized μλ¬ ν΄κ²°
λ€μμ μλ¬κ° λ°μνλ λΆλΆμ μ½λμ΄λ€.
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)
}
}