0

Kotlin error 'this' is not defined in this context and more errors - please help

Dear SoloLearners I am a Kotlin beginner and want to follow a video tutorial about making a record audio request function by a click of an simple button. Sadly, I am not able to run the code without any errors. I tried it since a few weeks but like I said: I am a beginner. Please, can someone help me? Here is the Code snipped and a picture of the errors: https://code.sololearn.com/ca20A12A14a2/?ref=app https://www.sololearn.com/post/907892/?ref=app Thank you for every help. kind regards Frederik

29th Jan 2021, 12:37 PM
Frederik
11 ответов
+ 5
Let's remove slideshowViewModel part of code and add to the onviewcreated(). override fun onViewCreated(view: View, savedInstanceState: Bundle?) { //Button button = findViewById(R.id.btn_send); //Clear this code from onviewcreate() slideshowViewModel= ViewModelProviders.of(this).get(SlideshowViewModel::class.java) slideshowViewModel.text.observe(this, Observer { }) btn_send.setOnClickListener { requestRecordAudioPermission() } } If it doesn't seems to resolve the problem try 'requestContext()' or 'activity!!' instead of 'this'.
30th Jan 2021, 3:48 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { //Button button = findViewById(R.id.btn_send); btn_send.setOnClickListener { requestRecordAudioPermission() } } Part -2
30th Jan 2021, 2:19 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
private fun hasRecordAudioPermission() = ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED private fun requestRecordAudioPermission(){ var permissionsToRequest = mutableListOf<String>() if(!hasRecordAudioPermission()) permissionsToRequest.add(Manifest.permission.RECORD_AUDIO) if(permissionsToRequest.isNotEmpty()){ ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(),0) } } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>, grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if(requestCode == 0 && grantResults.isNotEmpty()) { for(i in grantResults.indices) { if(grantResults [i] == PackageManager.PERMISSION_GRANTED) { Log.d("PermissionsRequest", "${permissions[i]} granted.") } } } } } Part-3
30th Jan 2021, 2:20 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
This will surely reduce some errors, click Listener part of the code should be placed inside onviewcreated() after view is created.but I still have little or no idea about the main problem of your code as I'm not familiar with kotlin. It took me a while to realize some basic things inside this code. But let's see how far can we go.
30th Jan 2021, 2:26 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
Frederik suggestion use ```kt // implement activity-ktx, fragment-ktx libs to gradle file val myViewMidel: SlideshowViewModel by viewModels ``` Instead of viewModelProvider which is deprecated And also Change ```kt viewMiodel.observe(getActivity, Observer{ }) ``` TO ```kt viewMiodel.observe(activity){ } ```
1st Feb 2021, 3:32 AM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 1
I'm not fully aware of what's happening with your code, but I can vividly point the onecreateview() part of the code. First of all SlideshowFragment is not activity it is a fragment, so 'this' keyword is violiting with code. Alternative is you can try getActivity() or an context, don't know it will work out well or failed, just replace the below code with your existing onconcreteview(). And see what happens. private lateinit var slideshowViewModel: SlideshowViewModel override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val slideshowViewModel = ViewModelProviders.of(getActivity()).get(SlideshowViewModel::class.java) val root = inflater.inflate(R.layout.fragment_edward, container, false) slideshowViewModel.text.observe(getActivity(), Observer { }) return root }
29th Jan 2021, 2:49 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
There is missing curlybrace in your button clicklistner. I recently figured out that kotlin deals differently with context, so instead of getActivity() we need requireActivity() . - Replace your code with this one Button button = findViewById(R.id.btn_send); btn_send.setOnClickListener { requestRecordAudioPermission() //TODO: solve error(s) } private lateinit var slideshowViewModel: SlideshowViewModel override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { slideshowViewModel = ViewModelProviders.of(requireActivity()).get(SlideshowViewModel::class.java) val root = inflater.inflate(R.layout.fragment_edward, container, false) slideshowViewModel.text.observe(requireActivity(), Observer { }) return root } If still errors remain, than I should look at your SlideshowViewModel class.
30th Jan 2021, 11:54 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
Try this : package com.example.testapp.ui.slideshow import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.app.ActivityCompat import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import com.example.testapp.R class SlideshowFragment : Fragment() { private lateinit var slideshowViewModel: SlideshowViewModel override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val slideshowViewModel = ViewModelProviders.of(getActivity()).get(SlideshowViewModel::class.java) val root = inflater.inflate(R.layout.fragment_edward, container, false) slideshowViewModel.text.observe(getActivity(), Observer { }) return root } Part-1
30th Jan 2021, 2:16 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
0
Thank you for your answer, 0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ . I replaced your Code and back then, I have diffrent (4) errors now. I added the button onclick listener, which I had previously just in comments and sadly, there are 27 errors now. I hope you can find another bug in this Code. Thank you so much that you said that the code would be for an activity. Not for a fragment. I didn't noticed that. Now, fore than half of the errors are comprehensible for me. Followed the new Code and the new post of the errors... https://www.sololearn.com/post/909368/?ref=app https://code.sololearn.com/ca20A12A14a2/?ref=app Thank you all again for every help.
30th Jan 2021, 8:45 AM
Frederik
0
Thank you. 0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ I really start to realize, that this "simple android template" is actually very difficult. Especially because I am a beginner. I hope that I will be able to learn to understand this whole Code once. However, here is the Code of the SlideshowViewModel.kt file: https://code.sololearn.com/cOd8mkkDb97T/?ref=app Following my error list: https://www.sololearn.com/post/909771/?ref=app
30th Jan 2021, 1:36 PM
Frederik
0
Wow! Thank you so much. I learn to understand the Code more and more. What exactly are those errors with the 'this'? I hope I can understand and solve those. https://www.sololearn.com/post/909880/?ref=app
30th Jan 2021, 3:05 PM
Frederik