开发者

Resources$NotFoundException in Android

开发者 https://www.devze.com 2023-04-11 02:51 出处:网络
The problem is a Resources$NotFoundException in the tv2.settext(), i don\'t know why i have a TreeMap, inside the pizza title and the count.

The problem is a Resources$NotFoundException in the tv2.settext(), i don't know why i have a TreeMap, inside the pizza title and the count. Please help!

Calling:

for (Pizza pizza : list) {

            if(map.containsKey(pizza.title))
            {

                int i = map.get(pizza.title);
                map.remove(pizza.title);

                map.put(pizza.title, i++);
            }
            else
            {
                map.put(pizza.title, 1);
            }
          }

Pizza:开发者_StackOverflow社区

public class Pizza implements Comparable<String> {

    public String title;
    public int rate;
    public String date;
    public Bitmap picture;
    public int id;
    @Override
    public int compareTo(String another) {
        // TODO Auto-generated method stub
        return this.title.compareTo(another);
    }

}

Error Code:

for (Entry<String, Integer> entry : map.entrySet()) {

            tv.setText(entry.getKey());
            tv2.setText((Integer)entry.getValue()); //Error occures here

            tv.setGravity(Gravity.LEFT);
            tv2.setGravity(Gravity.RIGHT);

            TableRow tr1 = new TableRow(this);
            tr1.addView(tv);
            tr1.addView(tv2);
            tl.addView(tr1, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            }
        }

Error:

10-04 09:04:40.475: ERROR/AndroidRuntime(954): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1


Change tv2.setText((Integer)entry.getValue()); to

tv2.setText(entry.getValue().toString());

do like this

 TableRow tr1 = new TableRow(this);

 for (Entry<String, Integer> entry : map.entrySet()) {

        tv.setText(entry.getKey());
        tv2.setText((Integer)entry.getValue()); //Error occures here

        tv.setGravity(Gravity.LEFT);
        tv2.setGravity(Gravity.RIGHT);

        tr1.addView(tv);
        tr1.addView(tv2);
        tl.addView(tr1, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        }
    }


You should use: tv2.setText(entry.getValue().toString());

Moreover, you do not need following line of code:

map.remove(pizza.title);

All you need to write is: map.put(pizza.title, i++); becaues "put" method of the Map replaces the old value.


I don't know what you are trying to do but you're obviously not using a R.string.my_title_or_so for setText. The Integer you pass to this method should be a valid (generated) id from your R class. Or you covert the number to a string e.g. num + "".


tv2.setText((Integer)entry.getValue());

Parse your Integer to String

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号