Reputation: 1
So, I have an activity with a bottomNavBar with 3 pages. Each of these pages is a fragment. In my app i need camera permission so i wrote some code to request this permission an check if the permission is granted.
Now there is a problem: I wrote this code a few months ago, when there was no navigation bar and no fragments. Logicall, I have requested permissions in the MainActivity.kt
file.
But now I request this permission in the fragment logic but it wont work.
The Compiler shows me the following error:
"Type mismatch: inferred type is home but Context
was expected" for the function checkCameraPermissions()
and:
"Type mismatch: inferred type is home but Activity
was expected" for the requestCameraPermission()
function
("home" is the name of the fragment and the class)
The problem lays in the "this" argument, its always red marked, seems like some kind of context problem.
Language: Kotlin Here is the original code:
private fun requestCameraPermission(){
// TODO request camera permission (for camera intent)
ActivityCompat.requestPermissions(this, cameraPermissions, CAMERA_REQUEST_CODE)
}
private fun checkCameraPermissions(): Boolean{
//check if camera permission is allowed, true if yes, false if no
val resultCamera = (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED)
//check if Storage permission is allowed, true if yes, false if no
val resultStorage = (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED)
//returns both results als false/true
return resultCamera
}
So I tried to fix it, the code editor doesn't shows any problems anymore but the app just keeps crashing if i try to open it. My solution (not working):
private fun requestCameraPermission(){
//request camera permission (for camera intent)
activity?.let { ActivityCompat.requestPermissions(it, cameraPermissions, CAMERA_REQUEST_CODE) }
}
private fun checkCameraPermissions(): Boolean{
//TODO check if it works
//check if camera permission is allowed, true if yes, false if no
val resultCamera = (activity?.let { ContextCompat.checkSelfPermission(it, Manifest.permission.CAMERA) }
== PackageManager.PERMISSION_GRANTED)
//check if Storage permission is allowed, true if yes, false if no
val resultStorage = (activity?.let { ContextCompat.checkSelfPermission(it, Manifest.permission.WRITE_EXTERNAL_STORAGE) }
== PackageManager.PERMISSION_GRANTED)
I already have googled and tried to move these functions to the activity logic but i need these functions in other parts of my code, so it wouldn't work out for me.
I am still new to Kotlin and mobile app development so i am glad to hear your answers and sorry for my English.
Upvotes: -1
Views: 68
Reputation: 1300
first of all, when you need to get the context in fragment you can use requierdContext()
method:
ActivityCompat.requestPermissions(requierdContext(), cameraPermissions, CAMERA_REQUEST_CODE).
Another thing is about exception, I believe in your case it's NullPointerException
and it's related to the place where you try to get the context or activity. Make sure when you access activity or context inside the fragment, the fragment is already attached to the activity(onAttach
method was called). To understand how it works you can check docs by the link
Upvotes: 0