错误java.lang.NullPointerException;在新线程中使用方法时

时间:2014-01-26 23:53:18

标签: java android multithreading nullpointerexception

我正在尝试使用以下代码来学习Android。我在线程内使用方法时遇到错误。我无法调试查看日志的问题。你能帮我理解我做错了吗?

类:EarthquakeListFragment

  public class EarthquakeListFragment extends ListFragment {

ArrayAdapter<Quake> aa;
ArrayList<Quake> earthquakes = new ArrayList<Quake>();

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    int layoutID = android.R.layout.simple_list_item_1;
    aa = new ArrayAdapter<Quake>(getActivity(), layoutID, earthquakes);
    setListAdapter(aa);
    Log.d("EarthQuake", "onActivityCreated");

    Thread t = new Thread(new Runnable() {
        public void run() {
        refreshEarthquakes();
        }
        });
        t.start();

}

private static final String TAG = "EARTHQUAKE";

private void refreshEarthquakes() {
    // Get The XML

    try {
        URL url;
        String quakeFeed = "http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml";
        url = new URL(quakeFeed);

        URLConnection connection;
        connection = url.openConnection();

        HttpURLConnection httpConnection = (HttpURLConnection) connection ; 
        int responseCode = httpConnection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream in = httpConnection.getInputStream();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            //Parse the earthquake feed
            Document dom = db.parse(in);
            Element docEle = dom.getDocumentElement();

            //Clear the old earthquakes
            earthquakes.clear();

            //Get a list of each earthquake entry 
            NodeList nl = docEle.getElementsByTagName("entry");
            if (nl != null && nl.getLength() > 0 ) {
                for (int i = 0; i < nl.getLength(); i++) {
                Element entry = (Element) nl.item(i);
                Element title = (Element) entry.getElementsByTagName("title").item(0);
                Element g = (Element) entry.getElementsByTagName("georss:point").item(0);
                Element when = (Element) entry.getElementsByTagName("updated").item(0);
                Element link = (Element) entry.getElementsByTagName("link").item(0);

                String details = title.getFirstChild().getNodeValue();
                String hostname = "http://earthquake.usgs.gov";
                String linkString = hostname + link.getAttribute("href");

                String point = g.getFirstChild().getNodeValue();
                String dt = when.getFirstChild().getNodeValue(); 
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
                Date qdate = new GregorianCalendar(0,0,0).getTime();

                try {
                    qdate = sdf.parse(dt);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                String[] location = point.split(" ");
                Location l = new Location("dummyGPS");
                l.setLatitude(Double.parseDouble(location[0]));
                l.setLongitude(Double.parseDouble(location[1]));

                String magnitudeString = details.split(" ")[1];
                int end = magnitudeString.length() -1 ;
                double magnitude = Double.parseDouble(magnitudeString.substring(0, end));

                details = details.split(",")[1].trim();
                Quake quake = new Quake(qdate, details, l, magnitude, linkString);

             // Process a newly found earthquake
                addNewQuake(quake);

                }
            }
        }


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

private void addNewQuake(Quake quake) {
    // Add the new quake to our list of earthquakes.
    earthquakes.add(quake);

    // Notify the array adapter of a change.
    aa.notifyDataSetChanged();
  }

}

类地震

    public class Earthquake extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("EarthQuake","Activity");
}

Class Quake

    public class Quake {
     private Date date;
     private String details;
     private Location location;
     private double magnitude;
     private String link;

     public Date getDate() { return date; }
     public String getDetails() { return details; }
     public Location getLocation() { return location; }
     public double getMagnitude() { return magnitude; }
     public String getLink() { return link; }

     public Quake(Date _d, String _det, Location _loc, double _mag, String _link) {
       date = _d;
       details = _det;
       location = _loc;
       magnitude = _mag;
       link = _link;
     }

     @Override
     public String toString() {
       SimpleDateFormat sdf = new SimpleDateFormat("HH.mm");
       String dateString = sdf.format(date);
       return dateString + ": " + magnitude + " " + details;
     }

   }    

错误日志

 01-26 18:37:37.561: D/EarthQuake(974): Activity
 01-26 18:37:37.561: D/EarthQuake(974): onActivityCreated
 01-26 18:37:37.611: D/dalvikvm(974): GC_FOR_ALLOC freed 49K, 4% free 2970K/3080K, paused 36ms, total 39ms
 01-26 18:37:37.761: D/gralloc_goldfish(974): Emulator without GPU emulation detected.
 01-26 18:37:38.492: W/dalvikvm(974): threadid=11: thread exiting with uncaught exception (group=0x41465700)
 01-26 18:37:38.492: E/AndroidRuntime(974): FATAL EXCEPTION: Thread-84
 01-26 18:37:38.492: E/AndroidRuntime(974): java.lang.NullPointerException
 01-26 18:37:38.492: E/AndroidRuntime(974):     at com.example.myearthquake.EarthquakeListFragment.refreshEarthquakes(EarthquakeListFragment.java:99)
 01-26 18:37:38.492: E/AndroidRuntime(974):     at com.example.myearthquake.EarthquakeListFragment.access$0(EarthquakeListFragment.java:58)
 01-26 18:37:38.492: E/AndroidRuntime(974):     at com.example.myearthquake.EarthquakeListFragment$1.run(EarthquakeListFragment.java:49)
 01-26 18:37:38.492: E/AndroidRuntime(974):     at java.lang.Thread.run(Thread.java:841)

感谢您的时间..

0 个答案:

没有答案