Android Studio private function Error ActivityCompat | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Android Studio private function Error ActivityCompat

Dear SoloLearn Community I develope an android app with a request record audio permission function which should be activated on an button click. Yet, I simply made a function which checks, if the user has the permission. private fun hasRecordAudioPermission() = ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) Here is the error I got: 'this' is not defined in this context. I don't know what to do now. Please, can you help me? Thank you.

13th Dec 2020, 8:09 AM
Gabriel
5 Answers
+ 2
Gabriel , You are defining that function `hasRecordAudioPermission()` outside class. Move the function inside class and see if it works. *fingers crossed*
13th Dec 2020, 8:33 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 2
Gabriel , Great! I was writing code to show you but you already fixed it ๐Ÿ˜๐Ÿ˜
13th Dec 2020, 8:43 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 1
Gabriel , not an expert in Android but maybe I can help ๐Ÿ˜ฌ Can you please provide complete code from that file? Post it in playground and share a link here. With given code I can't see what `this` might be refering to in that context.
13th Dec 2020, 8:15 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 1
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ found it! Thank you so much!
13th Dec 2020, 8:36 AM
Gabriel