Number formatting does not take into account locale settings. Consider using String.format instead android studio
Asked Answered
C

4

7

Here is my question and its solution regarding this problem..

Em trying to pass textview value between different activities, and em getting a problem. when i execute the code, the app crashes on opening StudentActivity, but then it shows the correct result..here is the code

LoginActivity.java

    int a = Integer.parseInt(textView.getText().toString());
    Intent i = new Intent(LoginActivity.this, StudentActivity.class);
    i.putExtra("level", a);
    startActivity(i);

StudentActivity.java

      textView.setText(Integer.toString(getIntent().getExtras().getInt("level")));

IN studentActivity, Integer.toString(getIntent().getExtras().getInt("level")) => this line says Number formatting does not take into account locale settings. Consider using String.format instead.Please suggest some code.. Any help would be truely appreciated!!

Cristobal answered 10/10, 2015 at 7:6 Comment(0)
B
12

Use

textView.setText(String.format(Locale.getDefault(), "%d", your_int));

The correct locale will automatically be used

Brickey answered 9/9, 2018 at 6:32 Comment(0)
S
10

Regarding the warning :

"Number formatting does not take into account locale settings. Consider using String.format instead android studio",

This is a Lint warning called "TextView Internationalization" which says :

When calling TextView#setText * Never call Number#toString() to format numbers; it will not handle fraction separators and locale-specific digits properly. Consider using String#format with proper format specifications (%d or %f) instead.

So you should have written :

 textView.setText(String.format("%d", getIntent().getExtras().getInt("level"))));
Stickup answered 5/1, 2016 at 16:27 Comment(2)
well you still didn't set locale here, no differencePeculium
You can call String.format(Locale.getDefaultLocale(), "%d", getIntent().getExtras().getInt("level"))) and locale is addedBuckinghamshire
C
1

In you LoginActivity, you insert String! named "a" into Intent.

In your StudentActivity, you try to grab Integer!! thanks for call getInt().

Simply change whole line to

textView.setText(getIntent().getExtras().getString("level"));
Cralg answered 10/10, 2015 at 7:14 Comment(3)
Hmm I've anyway never used such call over getExtras(). You may try use this getIntent().getStringExtra(). From source code it looks that result is almost same, but getExtras() always create a new instance of Bundle object, so it is also unneeded creating of objects. Anyway your code looks completely correctly and I have no idea why it should complain about locale when you play with integer number. Suggest to check different places in your code.Cralg
Sorry for a delay. Anyway you gave so little details. Generally, there is no place where should NullPointerException happen. 'getIntent' should be always non-null. GetStringExtra may return null, but such value set to TextView is valid! So please check your code, because problem is not on this line of code. Otherwise you have to post longer version of your code.Cralg
i've changed my format, i've used shared preference for passing dataCristobal
B
1

I used:

textView.setText(getIntent().getExtras().getString("level"));

but it still has some warning; when I use this:

textView.setText(Locale.getDefault(), getIntent().getExtras().getString("level"));

it is Ok;

Braeunig answered 19/6, 2017 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.