NumberFormatException的android系统,parseDouble的GeoPoint [英] NumberFormatException in android, parseDouble GeoPoint

查看:154
本文介绍了NumberFormatException的android系统,parseDouble的GeoPoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到这个问题与我的Andr​​oid应用

我现在这一段时间努力,并通过类似的线程很多了,但从来没有真正解决它。我没有设法重新坠毁我自己,但我得到来自其他用户的崩溃报告

这里是一个崩溃报告:

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

这里是我的code,的最后一行似乎是一个产生错误

的SAXParserFactory SPF = SAXParserFactory.newInstance();
        的SAXParser SP = spf.newSAXParser();
        XMLReader的XR = sp.getXMLReader();        / **发送URL解析XML标签* /
        网址sourceUrl =新的URL(
        http://www.mysite.com/xml.php);        / **创建处理程序来处理XML标签(扩展的DefaultHandler)* /
        MyXMLHandler myXMLHandler =新MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(新的InputSource(sourceUrl.openStream()));
        }赶上(例外五){
        //System.out.println(\"XML帕兴错误时抛出=+ e)条;
        }        sitesList = MyXMLHandler.sitesList;
        gotsnr =;
        gotmarkers = 0;
        LocationOverlay locationOverlay =新LocationOverlay(MapView类,getResources()getDrawable(R.drawable.polis),mContext。);        如果(sitesList!= NULL){
        的for(int i = 0; I< sitesList.getSnr()大小();我++){
        。gotsnr = sitesList.getSnr()获得(I)
        gotmarkers =(ⅰ);
        //如果(gotsnr!=){
        如果(sitesList.getLat()。得到(ⅰ).equalsIgnoreCase()|| sitesList.getLat()。得到(ⅰ).equalsIgnoreCase(空)){
            //空的,什么也不做        }其他{        的GeoPoint点=新的GeoPoint(新双(Double.parseDouble(sitesList.getLat()获得(I))* 1e6个电子).intValue(),新的双(Double.parseDouble(sitesList.getLng()获得(I)) * 1e6个电子).intValue());

* 下面replie编辑@kcoppock

难道这做的伎俩?

 双DLAT = 0;
双DLNG = 0;            如果(sitesList.getLat()获得(I)= NULL和放大器;!&安培;!等于(sitesList.getLat()获得(I))。){
                    尝试{
                       DLAT = Double.parseDouble(sitesList.getLat()获得(一));
                    }赶上(NumberFormatException的E){
                       //System.out.println(e.getMessage());
                    }
                }
            如果(sitesList.getLng()获得(I)= NULL和放大器;!&安培;!等于(sitesList.getLng()获得(I))。){
                尝试{
                    DLNG = Double.parseDouble(sitesList.getLng()获得(一));
                }赶上(NumberFormatException的E){
                   //System.out.println(e.getMessage());
                }
            }            如果(DLAT = 0&放大器;!&放大器;!DLNG = 0){            的GeoPoint点=新的GeoPoint(新双(DLAT * 1e6个电子).intValue(),新的双(DLNG * 1e6个电子).intValue());


解决方案

您不是摆在首位,这是即使你间没有不好的做法处理 NumberFormatException异常 有ŧ崩溃。拆分排队,先解析双打,并且一定要捕捉到 NumberFormatException异常并适当地处理它(比如抛出一个警告对话框,将默认值等)案件出于某种原因在值 getLat() getLon()不是可解析,这是显然是发生在这些情况下。

 双纬度= 0,经度= 0;
字符串SLAT = sitesList.getLat()得到(I)。
。字符串SLNG = sitesList.getLng()获得(I)
的GeoPoint点;尝试{
    纬度= Double.parseDouble(SLAT);
    LNG = Double.parseDouble(SLNG);    //如果送过来,他们成功地解析
    点=新的GeoPoint((INT)(LAT * 1e6个电子),(INT)(LNG * 1e6个电子));
}赶上(NumberFormatException的E){
    Log.e(TAG,无法解析纬度和/或经度);
}

Having this problem with my android app

I struggled with this for some time now, and went through alot of similar threads but never really solved it. I haven't managed to recreate the crash my self but i get crash-reports from other users

Here is a crash-report:

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)

And here is my code, the last line seem to be to one generating the error

        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());

* Edit below in replie to @kcoppock

Could this do the trick?

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());    

解决方案

You're not handling the NumberFormatException in the first place, which is bad practice even if you weren't having crashes. Split that line up, parse the doubles first, and be sure to catch the NumberFormatException and handle it appropriately (e.g. throw an alert dialog, place a default value, etc.) in case for some reason the value in getLat() or getLon() are not parseable, which is what is apparently happening in these cases.

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");
}

这篇关于NumberFormatException的android系统,parseDouble的GeoPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆