Android中的Resources$NotFoundException异常

6
问题出现在tv2.settext()中的Resources$NotFoundException,我不知道为什么会有一个TreeMap,在pizza标题和数量之间。请帮忙解决!
调用:
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);
            }
          }

披萨:

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);
    }

}

错误代码:

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));
            }
        }

错误:

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

23

tv2.setText((Integer)entry.getValue());改为:

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

这样做

 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));
        }
    }

10-04 09:22:40.585:ERROR/AndroidRuntime(1009):由于java.lang.IllegalStateException:指定的子项已经有一个父项。您必须首先在子项的父项上调用removeView()。 在tr1.addView(tv)中。 - user963395

3
你应该使用:tv2.setText(entry.getValue().toString()); 此外,你不需要以下代码行:
map.remove(pizza.title);

你只需要写:map.put(pizza.title, i++);,因为Map的“put”方法会替换旧值。

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

将您的整数解析为字符串


0
我不知道你想做什么,但显然你没有使用 R.string.my_title_or_so 来进行 setText。你传递给这个方法的 Integer 应该是你的 R 类中有效的(生成的)id。或者你可以将数字转换为字符串,例如 num + ""

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接