2

私は XmlPullParser サンプルを使用していますが、API 8 または 10 で実行すると値は null を返します (よくわかりません) が、他のバージョンの API 16 + では正常に動作します。何が起こっている可能性がありますか?null を返すパラメータは getAttributeValue(null, "name") です。

更新: AndroidManifest に android:minSdkVersion="8" を使用します

public class SitesXmlPullParser {

    static final String KEY_SITE = "site";

    public static List<StackSite> getStackSitesFromFile(Context ctx) {
        List<StackSite> stackSites;
        stackSites = new ArrayList<StackSite>();

        StackSite curStackSite = null;


        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();

            FileInputStream fis = ctx.openFileInput("StackSites.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

            xpp.setInput(reader);

            int eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = xpp.getName();

                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase(KEY_SITE)) {
                        curStackSite = new StackSite();
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase(KEY_SITE)) {
                        // This values return null
                        String name = xpp.getAttributeValue(null, "name");
                        String link = xpp.getAttributeValue(null, "link");
                        String about = xpp.getAttributeValue(null, "about");
                        String image = xpp.getAttributeValue(null, "image");
                        curStackSite.setName(name);
                        curStackSite.setLink(link);
                        curStackSite.setAbout(about);
                        curStackSite.setImgUrl(image);                      
                        stackSites.add(curStackSite);
                    }
                    break;

                default:
                    break;
                }
                eventType = xpp.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stackSites;
    }
}
4

0 に答える 0