Search code examples
javaandroidstringandroid-studiotextview

Wrong argument type for formatting argument


I use some codes like temp_out.setText(response.body().getCurrent().getTemp() + " ℃"); to get data from a weather API, but got two lint errors:

Do not concatenate text displayed with set text. Use resource placeholders only.

String literals in setText can not be translated. Use Android resources instead.

So i searched here for similar errors on this site and partly found a solution here https://stackoverflow.com/a/35053689/13899010, I used this one because I didn't want using resource String to change the data displayed. Added this to String.xml: <string name="blank">%d</string> then Changed my code to this:

temp_out.setText(getString(R.string.blank, response.body().getCurrent().getTemp() + " ℃"));

Now i get this error Wrong argument type for formatting argument '#1' in blank: conversion is 'd', received string (argument #2 in method call). I saw similar question Android Studio "Wrong argument type for formatting Error" in String.format() but his solution didn't work for me. How to fix this please?


Solution

  • make sure response.body().getCurrent().getTemp() returning a int, if its a string use %s in string blank

    you can try one of the following

    <string name="blank">%d Celsius</string>
    
    //invoking as follows
    getString(R.string.blank, response.body().getCurrent().getTemp())
    
    temp_out.setText("${response.body().getCurrent().getTemp()} Celsius")