2

这段代码有什么问题?我是 Android 的新手,我无法阅读我的 logcat 来找出问题所在。当我启动它时,我在 logcat 上收到很多启动错误。有人可以帮助我吗?

这是我的项目

这是我的 mainActivity 类:

package com.example.xmlparser;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

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 com.example.xmlparser.XMLGettersSetters;
import com.example.xmlparser.XMLHandler;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        /**
        * Create a new instance of the SAX parser
        **/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxP = saxPF.newSAXParser();
        XMLReader xmlR = saxP.getXMLReader();
        URL url = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
        /**
        * Create the Handler to handle each of the XML tags.
        **/
        XMLHandler myXMLHandler = new XMLHandler();
        xmlR.setContentHandler(myXMLHandler);
        xmlR.parse(new InputSource(url.openStream()));
        } catch (Exception e) {

        }

        XMLGettersSetters xml = XMLHandler.getXMLData();
        resetDisplay(xml.getTitle(), xml.getDate(), xml.getDescription(),  xml.getImageUrl());
}
        public void resetDisplay(String title,String date,String description,String imageUrl)
        {
    TextView tv1 = (TextView) findViewById(R.id.textview1);
        tv1.setText(title);
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setText(title);
    TextView tv3 = (TextView) findViewById(R.id.textView3);
    tv3.setText(title);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(getBitmapFromURL(imageUrl));
        }
        public  Bitmap getBitmapFromURL(String src) {
        try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
        } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
        } 
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
        }
        }

这是我的处理程序:

package com.example.xmlparser;

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



public class XMLHandler extends DefaultHandler {
String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;
public static XMLGettersSetters getXMLData() {
    return data;
}
public static void setXMLData(XMLGettersSetters data) {
    XMLHandler.data = data;
}
/**
 * This will be called when the tags of the XML starts.
 **/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    elementOn = true;
      if(localName.equals("item"))
      {
          data = new XMLGettersSetters();
      }
      else if(localName.equals("enclosure"))
            {
                  String attributeValue = attributes.getValue("url");
                  data.setImageUrl(attributeValue);


            }






}
/**
 * This will be called when the tags of the XML end.
 **/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    elementOn = false;
    /**
     * Sets the values after retrieving the values from the XML tags
     * */
    if (localName.equalsIgnoreCase("title"))
        data.setTitle(elementValue);
    else if (localName.equalsIgnoreCase("pubDate"))
        data.setDate(elementValue);
    else if (localName.equalsIgnoreCase("description"))
        data.setDescription(elementValue);

}
/**
 * This is called to get the tags value
 **/
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (elementOn) {
        elementValue = new String(ch, start, length);
        elementOn = false;
    }
}
}

这是我的 getter 和 setter 类:

package com.example.xmlparser;



import android.util.Log;

public class XMLGettersSetters {
private String title;
private String date;
private String description;
private String imageUrl;

public String getTitle() {
    return title;
}
public void setTitle(String Title) {
    this.title=Title;
    Log.i("This is the title:", Title);
}
public String getDate() {
    return date;
}
public void setDate(String Date) {
    this.date=Date;
    Log.i("This is the date:", Date);
}
public String getDescription() {
    return description;
}
public void setDescription(String Description) {
    this.description=Description;
    Log.i("This is the description:", Description);
}
public String getImageUrl() {
    return imageUrl;
}
public void setImageUrl(String ImageUrl) {
    this.imageUrl=ImageUrl;
    Log.i("This is the imageUrl:", ImageUrl);

}

}
4

0 回答 0