Skip to content Skip to sidebar Skip to footer

Cannot Resolve String Supplied To Vararg Parameter In Extension Function

strings.xml Showing your number: %1$s ActivityExt.kt fun Activity.showToast(textResId: Int, vararg formatArgs: String) { val text

Solution 1:

Use the spread operator:

fun Activity.showToast(textResId: Int, vararg formatArgs: String) {
    val text = getString(textResId, *formatArgs)
    Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}

Currently, you're passing an array as the format argument. By using the spread operator you pass the contents of the array as the format argument.

Solution 2:

You should use the spread operator to pass in the varargs to the getString function:

valtext= getString(textResId, *formatArgs)

This is because the type of formatArgs inside the showToast function is Array<String> (there's no vararg type or anything like that), and if you pass that in without the *, you'll only pass a single parameter, which will be the array instead of its contents.

Post a Comment for "Cannot Resolve String Supplied To Vararg Parameter In Extension Function"