English 中文(简体)
数字 外在 和机器人, 解析多词 GeoPoint
原标题:NumberFormatException in android, parseDouble GeoPoint
  • 时间:2012-05-22 21:08:32
  •  标签:
  • android

我和机器人应用程序有这种问题

我为此挣扎了一段时间, 经历了许多相似的线索, 但从未真正解决它。 我还没有设法重建崩溃的自我, 但我得到了来自其他用户的崩溃报告 。

<% 1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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@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());    
最佳回答

您一开始没有处理 < code> 数字格式例外 , 即便您没有崩溃, 这种做法也不好。 将该行向上拆分, 先解析双倍, 并确保抓住 < code> 数字格式例外 并适当处理它( 例如, 扔出警示对话框, 设置默认值等), 万一由于某种原因, 在 < code> getLat () 或 < code> 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");
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签