Android ArrayList方法不适用于参数

时间:2014-11-20 13:09:40

标签: java android arraylist

在我解析XML的方法中,我发现了两个错误,我对Android很陌生,并且很难理解它们的含义,甚至还没有用很多有用的解决方案进行搜索。

该行

animals = new ArrayList();

显示 - 类型安全:ArrayList类型的表达式需要未经检查的转换以符合ArrayList

和行

printAnimals(animals);

显示 - AnimalList类型中的方法printAnimals()不适用于参数(ArrayList)

以下是完整的参考方法

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<Animal> animals = null;
    int eventType = parser.getEventType();
    Animal currentAnimal = null;

    while (eventType != XmlPullParser.END_DOCUMENT)
    {
        String name = null;
        switch (eventType)
        {
            case XmlPullParser.START_DOCUMENT:
                animals = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name == "animal")
                {
                    currentAnimal = new Animal();
                } 
                else if (currentAnimal != null)
                {
                    if (name == "specificLocation")
                    {
                        currentAnimal.specificLocation = parser.nextText();
                    } 
                    else if (name == "name"){
                        currentAnimal.name = parser.nextText();
                    } 
                    else if (name == "location")
                    {
                        currentAnimal.location= parser.nextText();
                    }  
                    else if (name == "image")
                    {
                        currentAnimal.image= parser.nextText();
                    }  
                    else if (name == "animalInfo")
                    {
                        currentAnimal.animalInfo= parser.nextText();
                    }  
                    else if (name == "todaysFeed")
                    {
                        currentAnimal.todaysFeed= parser.nextText();
                    }  
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("animal") && currentAnimal != null){
                    animals.add(currentAnimal);
                } 
        }
        eventType = parser.next();
    }

    printAnimals(animals);
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您是否尝试使用类型声明ArrayList?

animals = new ArrayList<Animal>();