Unable to set EditText's text property using a String

Hello guys I am trying to make remember me button in kotlin but I cant run app because of this


Please rephrase the question so it would actually be a question. For example, try to read the error. Maybe if you read it, the question will vanish.

1 Like


1 Like

Luckily for you I ran into exactly the same issue here before. The short answer is that you either need to do emailEditText.setText(email) or do (emailEditText as TextView).text = email

But why does the original code give you an error and this code works? Basically, this stems from an issue in Kotlin where it assumes that getters return exactly the same type that the setters accept, which isn’t the case with the EditText apis. Basically, TextView has a getText and a setText that both accept a CharSequence, but EditText (which inherits from TextView) has an override for getText that returns an Editable. Kotlin here doesn’t recognise the fact that the method setText still can accept a CharSequence just fine because for some reason it follows the return type of the getter. It is honestly a mystery to me as to why it does that. But yeah then basically the solution is to either explicity call setText or to cast your EditText as a TextView so that the compiler believe that the text property is of type CharSequence.

Just as a tip for the future please try to explicitly include error messages and details like that in your question. Also, details about the feature that you’re trying to implement serve merely as a distractor for whoever is trying to answer your question. I’d personally say that you should try to narrow down the issue more and explain that what you’re trying to do is getting the email and password strings from SharedPreferences and then setting them in EditTexts but for some reason the compiler isn’t allowing you to set the text property of those EditTexts with a string value. Notice how all of this isn’t directly related to it being a remember me feature. Also if possible try to read the error messages themselves and then literally just be like “I thought that I can set the text with just a string but it’s saying that it’s expecting an Editable like whattttt??”. While this is informal it conveys the message quite well and helps to show what your intention is and what surprised you about the error so that people can then help you with it.