簡體   English   中英

資源$ Android中的NotFoundException

[英]Resources$NotFoundException in Android

問題是tv2.settext()中的Resources $ NotFoundException,我不知道為什么我在比薩餅標題和計數中有一個TreeMap。 請幫忙!

呼叫:

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

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

你應該使用: tv2.setText(entry.getValue().toString());

而且,您不需要以下代碼行:

map.remove(pizza.title);

你需要寫的只是: map.put(pizza.title, i++); 因為Map的“put”方法取代了舊值。

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

Integer解析為String

我不知道你要做什么,但你顯然沒有為setText使用R.string.my_title_or_so 傳遞給此方法的Integer應該是R類中的有效(生成)ID。 或者您將數字轉換為字符串,例如num + ""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM