0

我正在开发一个使用 SAX 解析器进行 XML 解析的 android 应用程序。我的 XML 目前在我的 WAMP 服务器上。在我的应用程序中,当我点击一个按钮时,我会看到一个标题列表,而在选择一个标题时,我会看到它的详细信息。我的问题是我的应用程序崩溃了。我搜索了很多,但找不到任何解决方案。当我从本地文件中获取我的 XML 数据时,它可以工作,但是当我从 URL 中获取它时,它会崩溃。我希望你能帮助我一些事情。

这是我的日志猫。

    07-13 17:30:14.874: E/AndroidRuntime(6844): FATAL EXCEPTION: main
    07-13 17:30:14.874: E/AndroidRuntime(6844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.svnitupdates/com.example.svnitupdates.UpdatesActivity}: java.lang.NullPointerException
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at                         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.os.Looper.loop(Looper.java:137)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at dalvik.system.NativeStart.main(Native Method)
    07-13 17:30:14.874: E/AndroidRuntime(6844): Caused by: java.lang.NullPointerException
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at com.example.svnitupdates.UpdatesActivity.onCreate(UpdatesActivity.java:66)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.Activity.performCreate(Activity.java:5104)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    07-13 17:30:14.874: E/AndroidRuntime(6844):     ... 11 more

这是我的更新活动。

    package com.example.svnitupdates;

    import java.net.URL;
    import java.util.ArrayList;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;


    public class UpdatesActivity extends Activity
    {
private ListView listview;
NewsList newsList = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.updates);

    listview = (ListView) findViewById(R.id.updatesListView);
    final ArrayList<String> list = new ArrayList<String>();

    try {


        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
      URL sourceUrl = new URL ("http://localhost/My%20files/data.xml");


        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler( myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));
        //xr.parse(new InputSource(is.getByteStream()));

        } catch (Exception e) {
        System.out.println("XML Parsing Excpetion = " + e);
        }


    newsList = MyXMLHandler.newsList;
        for (int i = 0; i < newsList.getTitle().size(); i++)
        {
            list.add(newsList.getTitle().get(i));
        }

    final UpdatesAdapter adapter = new UpdatesAdapter(UpdatesActivity.this , android.R.layout.simple_list_item_1, list);

    listview.setAdapter(adapter);

    listview.setItemsCanFocus(false);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {
          public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
          {
            Intent i = new Intent(UpdatesActivity.this, ArticleActivity.class);
            i.putExtra("s", position);
            startActivity(i);
          }
    }); 
  }
     }

这是我的 XMLHandler 类。

    package com.example.svnitupdates;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;

    public class MyXMLHandler extends DefaultHandler{
Boolean currentElement = false;
String currentValue = null;
public static NewsList newsList = null;

public static NewsList getNewsList() {
return newsList;
}

public static void setNewsList(NewsList newsList) {
MyXMLHandler.newsList = newsList;
}

/** Called when tag starts ( ex:- <name>AndroidPeople</name>
* -- <name> )*/
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {

currentElement = true;

if (localName.equals("maintag"))
{
/** Start */
newsList = new NewsList();
} 
}

/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
public void endElement(String uri, String localName, String qName)
throws SAXException {

currentElement = false;

/** set value */
if (localName.equalsIgnoreCase("title"))
newsList.setTitle(currentValue);

}

/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character ) */
public void characters(char[] ch, int start, int length)
throws SAXException {

if (currentElement) {
currentValue = new String(ch, start, length);
    currentElement = false;
}

}

   }

这是我的新闻列表类。

    package com.example.svnitupdates;

    import java.util.ArrayList;

    public class NewsList {
/** Variables */
private ArrayList<String> title = new ArrayList<String>();
//private ArrayList<String> news = new ArrayList<String>();

/** In Setter method default it will return arraylist
* change that to add */

public ArrayList<String> getTitle() {
return title;
}

public void setTitle(String title) {
this.title.add(title);
}

public ArrayList<String> getNews() {
    return title;
}

    public void setNews(String title) {
    this.title.add(title);
    }

public ArrayList<String> getChp() {
        return title;
        }

public void setChp(String title) {
        this.title.add(title);
        }

public ArrayList<String> getContacts() {
    return title;
    }

    public void setContacts(String title) {
    this.title.add(title);
    }
       }

这是我的文章活动。

    package com.example.svnitupdates;

    import java.net.URL;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;

    public class ArticleActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);
        Intent i = getIntent();
        savedInstanceState = i.getExtras();
        int k = savedInstanceState.getInt("s");
        updateArticleView(k);


    }

    public void updateArticleView(int position) {
    NewsList newslist = null;
    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL ("http://localhost/My%20files/data.xml");


        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandlerArticle myXMLHandler = new MyXMLHandlerArticle();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
        System.out.println("XML Parsing Excpetion = " + e);
        }


    newslist = MyXMLHandlerArticle.newsList;
    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setText(newslist.getNews().get(position));

     }

    }

我的 XML 文件在这里。

    <maintag>
    <item>
    <title>1st round interview of Student Council on 2nd April</title>
    <news>Student Council interviews on 2nd April. Students are requested to be on time. 1st round of interviews will be held in th Seminar Hall of respective department</news>
    </item>
    <item>
    <title>Interviews for 1st yearites for Drishti on 2nd April</title>
    <news>Interviews for drishti are being held on next monday. Those interested be present at sharp 5:00 PM at drishti room</news>
    </item>
    <item>
    <title>Workshop on Basic GUI Development by ACM on 3rd and 4th April</title>
    <news>Acm workshop on basic GUI. Those interested register on the link http://bit.ly/1073SMx. Install the required software from the link provided. Bring your laptops along(max 3 people/laptop).</news>
    </item>
    <item>
    <title>Registration for Treasure Hunt by CES starting from 5th April</title>
    <news>Registration for treasure hunt starting from 5th april. Prizes worth 15,000/- can be won.Do register and win.</news>
    </item>
    </maintag>
4

0 回答 0