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?”
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
Editable | Android Developers
.
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.
Eliote:
You can use android.widget.TextView#setText(java.lang.CharSequence) to set the text.
editText2?.setText(savedString)
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.
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