LWUIT和kXML Parser

时间:2010-08-23 01:49:59

标签: java-me lwuit kxml

Uncaught exception: java.lang.Error: Static initializer: java.lang.NullPointerException,   0
 - java.lang.Class.throwError(), bci=57
 - java.lang.Class.initialize(), bci=221
 - com.sun.lwuit.Component.<init>(), bci=5
 - com.sun.lwuit.Container.<init>(), bci=1
 - com.sun.lwuit.Form.<init>(), bci=8
 - com.sun.lwuit.Form.<init>(), bci=1
 - com.midlet.RSSMidlet.<init>(), bci=11
 - java.lang.Class.newInstance(), bci=0
 - com.sun.midp.main.CldcMIDletLoader.newInstance(), bci=46
 - com.sun.midp.midlet.MIDletStateHandler.createMIDlet(), bci=66
 - com.sun.midp.midlet.MIDletStateHandler.createAndRegisterMIDlet(), bci=17
 - com.sun.midp.midlet.MIDletStateHandler.startMIDlet(), bci=9
 - com.sun.midp.midlet.MIDletStateHandler.startMIDlet(), bci=4
 - com.sun.midp.appmanager.SelectorBase.run(), bci=33
 - java.lang.Thread.run(), bci=11

编码

package com.midlet;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
//import com.sun.lwuit.list.ListModel;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class RSSMidlet extends MIDlet implements ActionListener{

    public Vector feedVector;
    public String rssFeed; 
    private Command cmdSelect;
    private Command cmdExit;

    private Form form1 = new Form("Feeds List");
//  private Form form2 = new Form("Feed Description");

//  private Vector feed_Title;
    private List resultList;
//  private List feedTitle;
    public RSSModel model = new RSSModel();

    class ReadXML extends Thread{

        public void run(){
            try{
                rssFeed = "http://www.rottentomatoes.com/syndication/rss/top_news.xml";
                HttpConnection httpConnection = (HttpConnection) Connector.open(rssFeed);
                KXmlParser parser = new KXmlParser();
                parser.setInput(new InputStreamReader(httpConnection.openInputStream()));

                parser.nextTag();
                parser.require(XmlPullParser.START_TAG, null, null);
                while(parser.nextTag() != XmlPullParser.END_TAG){
                    parser.nextTag();
                    parser.require(XmlPullParser.START_TAG, null, "channel");
                    readXMLData(parser);
                    parser.require(XmlPullParser.END_TAG, null, "channel");
                }
                parser.require(XmlPullParser.END_TAG, null, "rss");
                parser.next();
                parser.require(XmlPullParser.END_DOCUMENT, null, null);
            }catch(Exception e){

            }
        }
    }

    public RSSMidlet() {
        // TODO Auto-generated constructor stub
        Display.init(this);
        Resources r;
        try{
            r = Resources.open("/LWUITtheme.res");
            UIManager.getInstance().setThemeProps(r.getTheme("LWUITDefault"));
        }catch(IOException e){
            //e.printStackTrace();
        }




        resultList = new List();
        for(int i=0; i<feedVector.size();i++){
            model = (RSSModel) feedVector.elementAt(i);
            resultList.addItem(model.getTitle().toString());
        }


        form1.addComponent(BorderLayout.CENTER,resultList);
        form1.addCommand(cmdExit);
        form1.addCommand(cmdSelect);
        form1.setScrollable(true);
        //form1.addCommandListener((ActionListener) this);
        form1.setTransitionInAnimator(Transition3D.createRotation(250, true));
        form1.show();
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub


        new ReadXML().start();
    }

    public void readXMLData(KXmlParser parser) throws IOException, XmlPullParserException{


        while(!"item".equals(parser.getName()) ){
            /** Check if document doesn't include any item tags */
            if( parser.next() == XmlPullParser.END_DOCUMENT )
                throw new IOException("No items in RSS feed!");
        }

        parser.require(XmlPullParser.START_TAG, null, "item");



        while(parser.nextTag() != XmlPullParser.END_TAG){

            parser.require(XmlPullParser.START_TAG, null, null);
            String name = parser.getName();
            String text = parser.nextText();
            //System.out.println ("<"+name+">"+text);

            if(name.equals("pubDate")){
                model.setPubDate(text);
            }else if(name.equals("title")){
                model.setTitle(text);
            }else if(name.equals("link")){
                model.setLink(text);
            }else if(name.equals("description")){
                model.setDescription(text);
            }
            parser.require(XmlPullParser.END_TAG, null, name);
        }
        feedVector.addElement(model);
        parser.require(XmlPullParser.END_TAG, null, "item");
    }

    public void actionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        if(evt.getCommand() == cmdExit){
            notifyDestroyed();
        }else if(evt.getCommand() == cmdSelect){

        }
    }
}

这是我的代码。我似乎无法使用kXML运行LWUIT。有一篇论坛帖子说我需要在Display.init(this)行之后初始化图形对象。但那条线应该在哪里呢?请帮助我...

1 个答案:

答案 0 :(得分:3)

Display.init(your_midlet)负责初始化LWUIT的显示,无需自己动手。只有限制是,您必须在使用任何表单之前初始化显示。 移动线

form1 = new Form("Feeds List")
行后

Display.init(this);

所以在初始化Display!

后初始化构造函数中的Form