0

我有一个Java类将有许多内部类。这样做是为了组织并将事物保存在单独的文件中。

public class PUCObjects
{

    public static class PUCNewsItem
    {

        public String title;
        public String summary;
        public String body;
        public String url;
        public String imageUrl;

    }

}

然后,我尝试创建该内部类的新实例(在另一个解析一些远程 XML 的类中执行此操作),但由于某种原因,它似乎没有被创建:

public static ArrayList<PUCObjects.PUCNewsItem> getPUCNews() throws IOException {

        String url = "http://api.puc.edu/news/list?key="+API_KEY+"&count=30";
        InputStream is = downloadUrl(url);
        XmlPullParserFactory pullParserFactory;

        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();
            parser.setInput(is, null);

            ArrayList<PUCObjects.PUCNewsItem> items = null;
            int eventType = parser.getEventType();
            PUCObjects.PUCNewsItem item = null;
            Log.d("Debug: ", "Start: "+url);
            while (eventType != XmlPullParser.END_DOCUMENT){
                String name = null;
                switch (eventType){
                    case XmlPullParser.START_DOCUMENT:
                        items = new ArrayList<PUCObjects.PUCNewsItem>();
                        break;
                    case XmlPullParser.START_TAG:
                        name = parser.getName();
                        //Log.d("Start Tag Name: ", parser.getName()+"  ===  "+name);
                        if (name == "item"){
                            Log.d("Debug: ", "Item");
                            item = new PUCObjects.PUCNewsItem();
                        } else if (item != null){
                            Log.d("Debug: ", "Item is not NULL 2");
                            if (name == "title"){
                                Log.d("Title: ", parser.nextText());
                                item.title = parser.nextText();
                            } else if (name == "summary"){
                                item.summary = parser.nextText();
                            } else if (name == "body_text"){
                                item.body = parser.nextText();
                            }  
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase("item") && item != null) {
                            Log.d("Debug: ", "ADD ITEM");
                            items.add(item);
                        }
                        break;
                }//end switch

                eventType = parser.next();

            }//end while

            Log.d("Debug: ", "Done");
            return items;

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }//end

我正在尝试创建对象,item = new PUCObjects.PUCNewsItem();但它似乎总是null.

有没有理由为什么这个对象没有被创建?

4

1 回答 1

5

问题是String比较。您的if陈述不是true由于==检查导致的。

if (name == "item"){

您需要在比较对象/字符串时使用equals()方法而不是。==阅读此线程以获取有关eqauals() 与 ==的更多信息

于 2013-09-04T17:26:31.167 回答