String.format
string doesn't match the XML format string
String.format
string doesn't match the XML format stringThis is an error.
| |
| |
Error | |
Correctness: Messages | |
Android | |
Android Open Source Project | |
Initial | |
Kotlin and Java files and resource files | |
This check runs on the fly in the IDE editor | |
2011 |
Here is an example of lint warnings produced by this check:
src/StringFormatMatches.java:6:Error: Wrong argument type for formatting
argument '#1' in score: conversion is 'd', received boolean (argument #2
in method call) (Did you mean formatting character b?)
[StringFormatMatches]
String output4 = String.format(score, true); // wrong
----
Here are the relevant source files:
res/values/formatstrings.xml
:
<resources>
<string name="score">Score: %1$d</string>
</resources>
src/StringFormatMatches.java
:
import android.app.Activity;
public class StringFormatMatches extends Activity {
public void test() {
String score = getString(R.string.score);
String output4 = String.format(score, true); // wrong
}
}
You can also visit the source code for the unit tests for this check to see additional scenarios.
You can suppress false positives using one of the following mechanisms:
// Kotlin
@Suppress("StringFormatMatches")
fun method() {
format(...)
}
or
// Java
@SuppressWarnings("StringFormatMatches")
void method() {
format(...);
}
//noinspection StringFormatMatches
problematicStatement()
lint.xml
file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="StringFormatMatches" severity="ignore" />
</lint>
Instead of ignore
you can also change the severity here, for
example from error
to warning
. You can find additional
documentation on how to filter issues by path, regular expression and
so on
here.
lintOptions {
disable 'StringFormatMatches'
}
In Android projects this should be nested inside an android { }
block.
lint
, using the --ignore
flag:
$ lint --ignore StringFormatMatches ...`