String.format string doesn't match the XML format string

String.format string doesn't match the XML format string

This is an error.

Id

StringFormatMatches

Summary

String.format string doesn't match the XML format string

Severity

Error

Category

Correctness: Messages

Platform

Android

Vendor

Android Open Source Project

Feedback

https://issuetracker.google.com/issues/new?component=192708

Since

Initial

Affects

Kotlin and Java files and resource files

Editing

This check runs on the fly in the IDE editor

Implementation

Source Code

Tests

Source Code

Copyright Year

2011

This lint check ensures the following: (1) If there are multiple translations of the format string, then all translations use the same type for the same numbered arguments (2) The usage of the format string in Java is consistent with the format string, meaning that the parameter types passed to String.format matches those in the format string.

Example

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.

Suppressing

You can suppress false positives using one of the following mechanisms:

formatted by Markdeep 1.18