0

我的 android 应用程序有这个问题

我已经为此苦苦挣扎了一段时间,并且经历了很多类似的线程,但从未真正解决它。我还没有设法重新创建我自己的崩溃,但我从其他用户那里得到了崩溃报告

这是一个崩溃报告:

java.lang.NumberFormatException: -
at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:149)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281)
at java.lang.Double.parseDouble(Double.java:318)
at polis.koll.HelloMapView.loadallmarkers(HelloMapView.java:665)
at polis.koll.HelloMapView.access$10(HelloMapView.java:611)
at polis.koll.HelloMapView$6.run(HelloMapView.java:587)
at java.lang.Thread.run(Thread.java:1027)

这是我的代码,最后一行似乎是生成错误的代码

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL(
        "http://www.mysite.com/xml.php");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));
        } catch (Exception e) {
        //System.out.println("XML Pasing Excpetion = " + e);
        }

        sitesList = MyXMLHandler.sitesList;
        gotsnr = "";
        gotmarkers = 0;
        LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), mContext);

        if (sitesList !=null){
        for (int i = 0; i < sitesList.getSnr().size(); i++) {
        gotsnr = sitesList.getSnr().get(i);
        gotmarkers = (i);
        //if (gotsnr !=""){ 
        if (sitesList.getLat().get(i).equalsIgnoreCase("")||sitesList.getLat().get(i).equalsIgnoreCase(null)){
            // Empty, do nothing

        } else{

        GeoPoint point = new GeoPoint( new Double(Double.parseDouble(sitesList.getLat().get(i)) * 1e6).intValue() , new Double(Double.parseDouble(sitesList.getLng().get(i)) * 1e6).intValue());

*编辑下方回复@kcoppock

这可以解决问题吗?

double dlat = 0;
double dlng = 0;

            if (sitesList.getLat().get(i) !=null && !"".equals(sitesList.getLat().get(i)) ){
                    try {
                       dlat = Double.parseDouble(sitesList.getLat().get(i));
                    } catch (NumberFormatException e) {
                       //System.out.println(e.getMessage());
                    }
                }
            if (sitesList.getLng().get(i) !=null && !"".equals(sitesList.getLng().get(i)) ){
                try {
                    dlng = Double.parseDouble(sitesList.getLng().get(i));
                } catch (NumberFormatException e) {
                   //System.out.println(e.getMessage());
                }
            }

            if (dlat !=0  && dlng !=0){

            GeoPoint point = new GeoPoint( new Double(dlat * 1e6).intValue() , new Double(dlng * 1e6).intValue());    
4

1 回答 1

2

你一开始就没有处理NumberFormatException,即使你没有崩溃,这也是不好的做法。拆分该行,首先解析双打,并确保捕获NumberFormatException并适当处理它(例如,抛出警报对话框,放置默认值等)以防由于某种原因该值getLat()getLon()不可解析,即在这些情况下显然发生了什么。

double lat = 0, lng = 0;
String sLat = sitesList.getLat().get(i);
String sLng = sitesList.getLng().get(i);
GeoPoint point;

try {
    lat = Double.parseDouble(sLat);
    lng = Double.parseDouble(sLng);

    //If it gets here, they parsed successfully
    point = new GeoPoint((int)(lat * 1e6), (int)(lng * 1e6));
} catch (NumberFormatException e) {
    Log.e("TAG", "Couldn't parse latitude and/or longitude");
}
于 2012-05-22T21:14:01.743 回答