Skip to content

TextView Doesn’t Like Integers

December 18, 2010

Hey everyone,

Super short post for now – was just coding something up for one of my new projects and ran into this error. I’ve encountered it before, but it’s been a while since I’ve seen it, and I’m not even sure how many other people run into this but just thought I would share it for those who might be scratching their heads.

First, let’s see who can identify the problem… I’ll provide the example in the same way that I encountered it just now – namely I was trying to pass some values around between classes:

public class A extends Activity {

private final int SELECT_CHAPTER_NUMBER = 1;

protected void onCreate(Bundle savedInstanceState) {

   // stuff
   Button chB = (Button) findViewById(;
   chB.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
           Intent i = new Intent(A.this, B.class);
           startActivityForResult(i, SELECT_CHAPTER_NUMBER);



    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == SELECT_CHAPTER_NUMBER) {
               int chBNum = data.getIntExtra("value", -1);

And here’s class B:

public class B extends Activity {

protected void onCreate(Bundle savedInstanceState) {
   // stuff

   EditText number = (EditText) findViewById(;
   submit.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                try {
                    int num = Integer.parseInt(number.getText().toString());
                    if (num > 0) {
                        Intent i = new Intent();
                        i.putExtra("value", num);
                        setResult(RESULT_OK, i);
                    } else {
                        Toast t = Toast.makeText(B.this, R.string.field_error, Toast.LENGTH_SHORT);
                } catch (Exception e) {
                    Toast t = Toast.makeText(B.this, R.string.field_error, Toast.LENGTH_SHORT);


Anyone see the problem yet? Well here’s the error log when I try to run this bad boy:

// 12-18 20:30:38.366: ERROR/AndroidRuntime(1625):  at android.widget.TextView.setText(
// 12-18 20:30:38.366: ERROR/AndroidRuntime(1625):  at android.content.res.Resources.getText(
// 12-18 20:30:38.366: ERROR/AndroidRuntime(1625): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1c

Howabout now?

Well, for those who haven’t caught on yet, basically what’s going on is that in the setText() method for TextView, it takes either a String, or an int. When it is passed a String the TextView will actually display the text on the phone itself – as expected. However, when passed an integer, normally us developers (at least in my experience) are used to methods automatically interpreting integers as Strings when they are to be either printed out or displayed somewhere as text. However in this case, the method looks something like TextView.setText(int res) where the integer is actually supposed to reference the id of a Resource String.

In other words, all the Strings that you place in the values/strings.xml file, along with any id’s that you declare in any of your layouts, etc, all get converted into hexadecimal integers which are automatically generated and placed into your gen/ file (again, for those who never bothered to check that file out that’s what those numbers mean). And so by passing in an integer like 5 into the setText() method of the TextView, the method will look through my gen/ file and look for the resource id with value 5, and in my case there was no such resource id with that value (luckily) and so it threw an error.

Again, I imagine that most people have probably seen this before or have done something to avoid it by chance, but for those who have seen it but never figured out what was going on, or for those who have never seen it here you go – why TextViews don’t like integers haha.

But yes, the fix? Simple:


And now it works fine.

Again, short, not meant to be super educational, just something I’ve encoutered many times now and thought might be neat to share.

Happy coding everyone.

– jwei

11 Comments leave one →
  1. December 18, 2010 6:32 pm

    I encountered this problem as well this week! Same conclusion there!

  2. Tanmay Mandal permalink
    December 18, 2010 9:18 pm

    More easy fix available



    • December 18, 2010 9:28 pm

      Yea basically any operation that will convert it to a String

      – jwei

  3. mad permalink
    December 19, 2010 2:01 am

    thank you, magic !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  4. Joshua Sigar permalink
    December 20, 2010 8:16 am

    Never encountered this problem but probably because I never write only number in textview. Or probably because I always read the method description when I use it the first time.

  5. February 4, 2011 3:36 pm

    Thank you very much.
    You saved me…..


  6. August 7, 2011 9:34 am

    THnks that was helpfull!

  7. Daniel González permalink
    March 22, 2012 2:21 am

    Really nice help. If I don’t find your post, problably now, I would have no neurons.

    Thank you.

  8. January 26, 2013 5:22 am

    greAT JOB BOY 🙂


  1. Tweets that mention TextView Doesn’t Like Integers « Think Android --

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: