Unresolved reference: TextView then Overload resolution ambiguity

I’m trying to update the Controller layer according to Listing 2.6 Wiring up the TextView.

When I build, I get an error
Unresolved reference: TextView

I then add an import for android.widget.TextView and get a new error:

Overload resolution ambiguity: 
public final fun setText(text: CharSequence!): Unit defined in android.widget.TextView
public final fun setText(resid: Int): Unit defined in android.widget.TextView

I’m new to Kotlin but not to programming–can’t find the right syntax to make it work (or what to check in Android Studio).

I was able to workaround the issue by changing my function to (add as Int):

    private fun updateQuestion() {
        val questionTextResId = questionBank[currentIndex].textResId
        questionTextView.setText(questionTextResId as Int)

@buzzsurfr this is a strange one. How are you declaring the TextView variable?

Is it like so?

private lateinit var questionTextView: TextView

Also ensure it’s an instance variable and not a local variable.

Also how does you Question class look like?

Make sure the textResId field in your Question class is of type Int and annotated with the @StringRes annotation like so:-

data class Question(@StringRes val textResId: Int, val answer: Boolean)

I believe the compiler is having an issue resolving textResId hence its not sure which setText method to execute as the method is overloaded.

1 Like

I am declaring the TextView variable using (class also shown for location):

class MainActivity : AppCompatActivity() {
    private lateinit var questionTextView: TextView

Question class (does have type Int with @StringRes annotation):

package com.bignerdranch.android.geoquiz
import androidx.annotation.StringRes
data class Question(@StringRes val textResId: Int, val answer: Boolean)
1 Like

Not sure how or why, but was able to fix the problem by changing the target SDK version to 28 and then Syncing the project to Gradle files. However, I also changed the SDK version back to 30 and sync’d again, and it works.

So in other words, we’ll blame either the computer or the operator. :wink:

In case anyone finds this, big things to check are the imports.

1 Like