0

我在每一行都有一个带有 gps 坐标的文件:

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("4:",point_t[0] + " - " + point_t[1]); 之后我得到了 NullPointerException

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 new 中使用坐标创建文件并现在使用它:

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

文件有错误

4

3 回答 3

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

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

于 2012-06-20T09:27:41.250 回答
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");
    }

编辑:或者它可能首先在“;”的基础上分割你的字符串,然后在你放入文件中的空间基础上

于 2012-06-20T09:33:33.553 回答
0

首先,您必须获取所有行 String[]。然后,如您所见,您必须用“”(空格)拆分以获得两个坐标,然后将这些字符串再次用“,”拆分以获得 logtitude 和 latitute。所以用一条线你会得到两个坐标,而不是一个

还有,这部分

        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
}

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

于 2012-06-20T10:23:48.137 回答