将字符串转换为int,创建一个地理点

时间:2012-06-20 09:12:10

标签: android string int geo

我的每行都有一个gps coords的文件:

9.34669876098644,48.2405319213867
9.36384963989269,48.2296714782715
9.3639497756958,48.2259712219238
9.36956977844238,48.2260589599609
9.36404991149908,48.2222709655763
9.36975955963135,48.2186508178712

现在我想解析文件并制作地理位置点。首先,我打开文件并读取行,而不是分割行,并希望将字符串设置为int。但是我在Log.d之后得到了NullPointerException(“4:”,point_t [0] +“ - ”+ point_t [1]);

try{
    InputStream koord = getAssets().open("bib.txt");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        Log.d("2: ", "Read File");
        String line;
        while (( line = buffreader.readLine()) != null) {
            Log.d("3: ", line);
            String[] point_t = line.split(",");
            Log.d("4:",point_t[0] + " - " + point_t[1]);
            int x = Integer.parseInt(point_t[0]);
            int y = Integer.parseInt(point_t[1]);
            Log.d("Geopoint:",x + " - " + y);
            points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
            Log.d("5:","saved");
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    // TODO: handle exception
}  

06-20 08:45:21.701: E/ActivityThread(231): Failed to find provider info for com.google.settings
06-20 08:45:21.711: E/ActivityThread(231): Failed to find provider info for com.google.settings
06-20 08:45:21.721: E/ActivityThread(231): Failed to find provider info for com.google.settings
06-20 08:45:21.971: D/dalvikvm(231): GC freed 3008 objects / 190136 bytes in 60ms
06-20 08:45:22.071: D/Checkpoint:(231): Start ArrayList
06-20 08:45:22.081: I/global(231): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
06-20 08:45:22.081: D/Checkpoint:(231): Read File
06-20 08:45:22.081: D/Checkpoint:(231): 9.34669876098644,48.2405319213867
06-20 08:45:22.081: D/Point:(231): 9.34669876098644 - 48.2405319213867
06-20 08:45:22.161: I/MapActivity(231): Handling network change notification:CONNECTED
06-20 08:45:22.171: E/MapActivity(231): Couldn't get connection factory client
06-20 08:45:22.381: D/dalvikvm(231): GC freed 983 objects / 342808 bytes in 62ms
06-20 08:45:22.391: D/AndroidRuntime(231): Shutting down VM
06-20 08:45:22.401: W/dalvikvm(231): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-20 08:45:22.401: E/AndroidRuntime(231): Uncaught handler: thread main exiting due to uncaught exception
06-20 08:45:22.425: E/AndroidRuntime(231): java.lang.NullPointerException
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.google.android.maps.MapView.onDraw(MapView.java:476)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6535)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6641)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6538)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.View.draw(View.java:6538)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.os.Looper.loop(Looper.java:123)
06-20 08:45:22.425: E/AndroidRuntime(231):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 08:45:22.425: E/AndroidRuntime(231):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 08:45:22.425: E/AndroidRuntime(231):  at java.lang.reflect.Method.invoke(Method.java:521)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 08:45:22.425: E/AndroidRuntime(231):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 08:45:22.425: E/AndroidRuntime(231):  at dalvik.system.NativeStart.main(Native Method)
06-20 08:45:22.442: I/dalvikvm(231): threadid=7: reacting to signal 3
06-20 08:45:22.461: I/dalvikvm(231): Wrote stack trace to '/data/anr/traces.txt'
06-20 08:45:24.701: I/Process(231): Sending signal. PID: 231 SIG: 9

@stuckless

Log.d("1: ", "Start ArrayList");
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
    InputStream koord = getAssets().open("bib.txt");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        Log.d("2: ", "Read File");
        String line;
        while (( line = buffreader.readLine()) != null) {
            Log.d("3: ", line);
            String[] point_t = line.split(",");
            Log.d("4:",point_t[0] + " - " + point_t[1]);
            double x = Double.parseDouble(point_t[0]);
            double y = Double.parseDouble(point_t[1]);
            Log.d("Geopoint:",x + " - " + y);
            points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
            Log.d("5:","saved");
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    Log.e("APP","Failed", e);
    }        
    mapView.getOverlays().clear();
    mapView.getOverlays().add(polygon);
    mapView.invalidate();
}

错误:

06-20 09:47:27.382: E/ActivityThread(324): Failed to find provider info for com.google.settings
06-20 09:47:27.382: E/ActivityThread(324): Failed to find provider info for com.google.settings
06-20 09:47:27.392: E/ActivityThread(324): Failed to find provider info for com.google.settings
06-20 09:47:27.702: D/dalvikvm(324): GC freed 3148 objects / 195472 bytes in 68ms
06-20 09:47:27.822: D/1:(324): Start ArrayList
06-20 09:47:27.822: I/global(324): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
06-20 09:47:27.822: D/2:(324): Read File
06-20 09:47:27.822: D/3:(324): 9.34669876098644,48.2405319213867
06-20 09:47:27.822: D/4:(324): 9.34669876098644 - 48.2405319213867
06-20 09:47:27.952: E/APP(324): Failed
06-20 09:47:27.952: E/APP(324): java.lang.NumberFormatException: 
06-20 09:47:27.952: E/APP(324):     at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
06-20 09:47:27.952: E/APP(324):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
06-20 09:47:27.952: E/APP(324):     at java.lang.Double.parseDouble(Double.java:285)
06-20 09:47:27.952: E/APP(324):     at de.bodprod.rettinfo.BOSLstItemDetail.onCreate(BOSLstItemDetail.java:93)
06-20 09:47:27.952: E/APP(324):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-20 09:47:27.952: E/APP(324):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:47:27.952: E/APP(324):     at android.os.Looper.loop(Looper.java:123)
06-20 09:47:27.952: E/APP(324):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 09:47:27.952: E/APP(324):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:47:27.952: E/APP(324):     at java.lang.reflect.Method.invoke(Method.java:521)
06-20 09:47:27.952: E/APP(324):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 09:47:27.952: E/APP(324):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 09:47:27.952: E/APP(324):     at dalvik.system.NativeStart.main(Native Method)
06-20 09:47:28.043: I/MapActivity(324): Handling network change notification:CONNECTED
06-20 09:47:28.052: E/MapActivity(324): Couldn't get connection factory client
06-20 09:47:28.272: D/dalvikvm(324): GC freed 1440 objects / 382480 bytes in 66ms
06-20 09:47:28.292: D/AndroidRuntime(324): Shutting down VM
06-20 09:47:28.292: W/dalvikvm(324): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-20 09:47:28.292: E/AndroidRuntime(324): Uncaught handler: thread main exiting due to uncaught exception
06-20 09:47:28.302: E/AndroidRuntime(324): java.lang.NullPointerException
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.google.android.maps.MapView.onDraw(MapView.java:476)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6535)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6641)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6538)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.View.draw(View.java:6538)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.os.Looper.loop(Looper.java:123)
06-20 09:47:28.302: E/AndroidRuntime(324):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-20 09:47:28.302: E/AndroidRuntime(324):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:47:28.302: E/AndroidRuntime(324):  at java.lang.reflect.Method.invoke(Method.java:521)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-20 09:47:28.302: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-20 09:47:28.302: E/AndroidRuntime(324):  at dalvik.system.NativeStart.main(Native Method)
06-20 09:47:28.323: I/dalvikvm(324): threadid=7: reacting to signal 3

解决方案:

现在它可以工作,我用eclipse中的coords创建文件并立即使用它:

ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
    InputStream koord = getAssets().open("biberach");
    if (koord != null) {
        InputStreamReader input = new InputStreamReader(koord);
        BufferedReader buffreader = new BufferedReader(input);
        String line;
        while (( line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
                double y = Double.parseDouble(point_t[0]);
                double x = Double.parseDouble(point_t[1]);
                points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
        }
        koord.close();
        polygon = new Polygon(points);
    }
}catch (Exception e) {
    Log.e("APP","Failed", e);
} 

文件错误

3 个答案:

答案 0 :(得分:1)

由于您正在捕获并忽略该块中的任何异常,请尝试在空捕获区域中注销该异常,并且您将看到您获得了NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "9.34669876098644"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at TestParseInt.main(TestParseInt.java:4)

你不能parseInt一个双号(即使它确实有效,你只会得到整数部分,这不是你想要的)。请尝试使用Double.parseDouble()

换句话说

        double x = Double.parseDouble(point_t[0].trim());
        double y = Double.parseDouble(point_t[1].trim());

而不是这个

}catch (Exception e) {
    // TODO: handle exception
}

尝试使用

}catch (Exception e) {
    Log.e("APP","Failed", e);
}

<强> UPDATE1: 也许确保你删除了空格以便使用 关于字符串的split("\\s*,\\s*)然后.trim()。但是Double.parseDouble()应该允许空格。

<强> UPDATE2:locale中,decimal是有效的分隔符,还是comma?如果是,那么您可能必须使用DecimalFormat来解析值并强制US区域设置

我从您的个人资料中看到您在德国,但您要解析的数字不是德语。所以,试试吧..

DecimalFormat parser = (DecimalFormat)DecimalFormat.getInstance(Locale.US);
double x = (Double)parser.parse("48.2405319213867");

这会强制解析器使用美国语言环境来解析数字(即,。是十进制)。

答案 1 :(得分:1)

你做错了。在你的文本文件字符串中,首先在空格的基础上填充它,然后将“;”的基础作为:

    String line="";
    while (( line = buffreader.readLine()) != null) {
        Log.d("3: ", line);
        line +=line;
    }
    String[] point_arr = line.split(" ");
    for (int i = 0; i <= point_arr.Length - 1; i++)
    {
        String[] point_t = point_arr[i].split(",");
        Log.d("4:",point_t[0] + " - " + point_t[1]);
        int x = Integer.parseInt(point_t[0]);
        int y = Integer.parseInt(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
        Log.d("5:","saved");
    }

编辑或者可能首先将您的字符串拆分为“;”,然后将空间的基础分开文件

答案 2 :(得分:0)

首先,你必须获得所有行String []。然后,如你所见,你必须用“”(空格)分割得到两个坐标,然后那些得到的字符串再次被“,”分开,以获得逻辑和替代。因此,使用一行,您将获得两个coords,而不是一个

此外,这部分

        int x = Integer.parseInt(point_t[0]);
        int y = Integer.parseInt(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));

不好,应该是:

        double x = Double.parseDouble(point_t[0]);
        double y = Double.parseDouble(point_t[1]);
        Log.d("Geopoint:",x + " - " + y);
        points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));

因为,当你有20.123456号码时,你得到20000000,而我的方法得到20123456

另外,在catch之前,再添加一个catch

catch(NumberFormatException e) {
    //this occurs when can't convert String to specified number format, so it means, that you give bad number to parse
}

它没有回答您的确切问题,但它可以帮助您完成工作