Android ArrayList.size返回notfound

时间:2012-12-23 23:18:35

标签: android vector size

我的应用为向量添加一个整数值,然后想知道该向量的大小。

if(nearSelected||middleSelected||farSelected){
    ArrayList<Integer> distance = new ArrayList<Integer>();

    //Which distance(s) has the user selected?
    if(nearSelected){distance.add(1);}
    if(middleSelected){distance.add(2);}
    if(farSelected){distance.add(3);}       

    //Attempt to display the number of choices picked to the user
    try {
        Toast.makeText(getBaseContext(), distance.size(), Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
       Log.i(LOG_TAG, e.toString());
    }
}

不幸的是,尝试获取distance.size()会导致NotFoundException。代码的所有其他部分运行正常,只是这部分崩溃。我搞砸了哪里?

1 个答案:

答案 0 :(得分:6)

Distance.size()不是导致Resources.NotFoundException及其Toast.makeText的原因。当使用 integer 作为参数调用时,它会查找字符串资源,并将整数作为id。如果您想将数字显示为字符串,那么您必须告诉它:

Toast.makeText(getBaseContext(), Integer.toString(distance.size()), Toast.LENGTH_SHORT).show();