0

由于此异常,我的 Android 应用程序崩溃,我不明白为什么会发生此异常:

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList

下面是我的代码。

private void setAllData(View v)
{
    ArrayList<String> idList = new ArrayList<String>();
    boolean added=false;
    for(String chid:checkedList)
    {
        SortedSet<String> ts = new TreeSet<String>();
        int id=txt2Id.get(chid);
        View v1=v.findViewById(id);
        if(v1!=null)
        {
            Log.e("---- Value of v1 ----", v1.getTag().toString());

            // I am getting ClassCast exception on the below line

            ArrayList<String>vidList=(ArrayList<String>) v1.getTag(id);

            if(vidList==null||vidList.size()==0)
            {
                ((CustomFragment) getActivity()).updateTab4List(new ArrayList<String>());
                return;
            }
            else
            {
                if(!added)
                {
                    idList=vidList;
                    added=true;
                }
                else if(idList.size()==0)
                {
                    ((CustomFragment) getActivity()).updateTab4List(idList);
                    return;
                }
                else
                {
                    ArrayList<String> temp = new ArrayList<String>();
                    int i=0;
                    for(String str:idList)
                    {
                        if(vidList.contains(str))
                            temp.add(str);
                    }
                    idList=temp;
                }
            }
        }
    }
    if(checkedList.size()==0)
    idList=null;
    ((CustomFragment) getActivity()).updateTab4List(idList);
}
4

1 回答 1

4

getTag()类的方法View返回一个对象。请参阅http://developer.android.com/reference/android/view/View.html#getTag()

您不能将其转换为 ArrayList。表达式不合法

 ArrayList<String>vidList=(ArrayList<String>) v1.getTag(id);

也许你想要

 ArrayList<String> vidList = new ArrayList<String>();
 vidList.add((String) v1.getTag(id));
于 2013-02-12T05:04:51.597 回答