Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'this@ActivityName' is not captured error Android/Kotlin

I'm repairing my friend's code and got confused.
My friend wants to fetch entered text (in EditText). Seems easy, right? Well, it is but instead of user input, he gets this warning/error:
enter image description here

To be honest I'm not sure how to fix it. He is coding in Kotlin (Android 10).
Activity that contains EditText: enter image description here

And XML:
enter image description here

This is how it looks when debugging:
enter image description here

The app started working just fine after running "File -> invalidate Cashes/Restart" option, I just don't understand where this warning came from and how to fix it because the error remained unchanged (even though the app works). Do you have an idea how to solve it?

All the best!

like image 352
Macieg Avatar asked Jun 07 '21 14:06

Macieg


People also ask

How do I use the is operator in Kotlin?

Use the is operator or its negated form !is to perform a runtime check that identifies whether an object conforms to a given type: In most cases, you don't need to use explicit cast operators in Kotlin because the compiler tracks the is -checks and explicit casts for immutable values and inserts (safe) casts automatically when necessary:

What is the unsafe cast operator in Kotlin?

Usually, the cast operator throws an exception if the cast isn't possible. And so, it's called unsafe. The unsafe cast in Kotlin is done by the infix operator as. Note that null cannot be cast to String, as this type is not nullable. If y is null, the code above throws an exception.

Why is my mainactivity not showing up in Android Studio?

This issue occurs because Android Studio was not able to detect the default MainActivity in your Android Studio Project. In this article, we will take a look at four different ways with which we can fix this error in your Android Project.

What is type erasure in Kotlin?

Type erasure and generic type checks Kotlin ensures type safety for operations involving generics at compile time, while, at runtime, instances of generic types don't hold information about their actual type arguments. For example, List<Foo> is erased to just List<*>.


2 Answers

fyi lambda expression like setOnClickListener from kotlin is not debuggable, see here.

if you want to debug variables inside setOnClickListener you should use the normal one e.g. setOnClickListener(object: View.OnClickListener {..})

like image 52
nashihu Avatar answered Sep 27 '22 18:09

nashihu


sometimes there will be problem in auto generated binding files, if so it will be solved after invalidate cache and restart ide. sometimes the warning/error show but the project and complied without errors. so no need to worry about that. for next time post the code as code not screen shots.

like image 25
vignesh Avatar answered Sep 27 '22 17:09

vignesh