Google Maps android API,在循环中绘制折线

时间:2013-01-02 09:50:31

标签: java android google-maps

我有两个字符串数组,latitudelongitude 我想在地图上绘制折线,documentation说,它就像馅饼一样简单。它是。如果我像这样手动添加折线,它可以工作:

PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))
        .add(new LatLng(37.45, -122.2)) 

但是当我试图循环添加折线时问题就开始了:

   rectOptions = new PolylineOptions();
    for(int i = 0; i<latitude.length(); i++){
rectOptions.add(new LatLng(Long.getLong(latitude[i]), Long.getLong(longitude[i])));
// Log.d("coordinates", latitude[i]+"|"+longitude[i]);
    }

我得到java.lang.NullPointerException我不知道为什么。我知道问题出在rectOptions.add()方法中,因为Log.d("coordinates", latitude[i]+"|"+longitude[i]);在同一个循环中工作正常。

这是LogCat的输出:

    01-02 11:59:27.380: E/AndroidRuntime(25146): FATAL EXCEPTION: main
01-02 11:59:27.380: E/AndroidRuntime(25146): java.lang.NullPointerException
01-02 11:59:27.380: E/AndroidRuntime(25146):    at com.shniv.MainActivity$1.onClick(MainActivity.java:82)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at android.view.View.performClick(View.java:3519)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at android.view.View$PerformClick.run(View.java:14140)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at android.os.Handler.handleCallback(Handler.java:605)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at android.os.Looper.loop(Looper.java:137)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at android.app.ActivityThread.main(ActivityThread.java:4456)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at java.lang.reflect.Method.invoke(Method.java:511)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
01-02 11:59:27.380: E/AndroidRuntime(25146):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案