Required Editable! found String? error


#1

Hi there i am very very new to programming but this is the error I am getting. Here is my code…
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
val savedString = savedInstanceState?.getString(TEXT_CONTENTS, “”)
editText2?.text = savedString
}

The final savedString expression I am getting the error “Required Editable! found String?”


#2

I guess you are developing on android. The problem is that editText2?.text is of type Editable and not of type string so you can’t pass a string. I would take a look at https://developer.android.com/reference/android/text/Editable.
What you need to do as far as I can tell is

Editable.Factory.getInstance().newEditable(savedString)

Addition: My answer should probably work (I think) but I am not an android developer. Eliote’s answer below shows a better way to solve this.


#3

You can use android.widget.TextView#setText(java.lang.CharSequence) to set the text.

editText2?.setText(savedString)

#4

This is for sure the better way. I am not an android developer and just looked up the definition of Editable.


#5

And FYI in the case of TextView you have to actually use setText method not setting it using “.text =” like you can with so many other bean properties because it has multiple overloads of setText and Kotlin decides that it is not a simple bean property.


#6

Great work


#7
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
  super.onRestoreInstanceState(savedInstanceState)
  val savedString = savedInstanceState?.getString(TEXT_CONTENTS, “”)
  editText2?.text = savedString
}

Your are using a nullable string instead a not null string. If savedInstanceState is null, savedString will be null. So, if we add elvis operator and return a empty string, it would work.

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
  super.onRestoreInstanceState(savedInstanceState)
  val savedString = savedInstanceState?.getString(TEXT_CONTENTS, "") ?: ""
  editText2?.text = savedString
}