1

这是我项目的一部分。我ArrayList从上一堂课中得到了经度和纬度。如果没有标记代码,应用程序可以正常工作,但是当我添加标记代码时,应用程序由于 NullPointerException 而终止。谁能告诉我为什么?

package xyz;

import...

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Route extends Activity {
GoogleMap map;
PolylineOptions polylineOptions;
ArrayList<Double> longitude,latitude;


@SuppressWarnings("unchecked")
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    double s_latitude = latitude.get(0);
    double s_longitude=longitude.get(0);


    /* If I add this line the app is terminating.
      map.addMarker(new MarkerOptions()
     .position(new LatLng(s_latitude,s_longitude))
    .title("Start Point"));
    */

    longitude = (ArrayList<Double>) getIntent().getSerializableExtra("Longitude");
    latitude = (ArrayList<Double>) getIntent().getSerializableExtra("Latitude");

    CameraUpdate center=
            CameraUpdateFactory.newLatLng(new LatLng(latitude.get(0),
                    longitude.get(0)));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

        map.moveCamera(center);
        map.animateCamera(zoom);



        polylineOptions=new PolylineOptions();

   for(int i=0;i<latitude.size();i=i+5)
   {
       polylineOptions.add(new LatLng(latitude.get(i), longitude.get(i)));
   }

       polylineOptions.color(Color.BLUE);
       polylineOptions.width(10);

        polylineOptions.geodesic(false);


        map.addPolyline(polylineOptions);


}
}

这是我的 LogCat:

11-10 21:15:14.027: E/AndroidRuntime(25929): FATAL EXCEPTION: main
11-10 21:15:14.027: E/AndroidRuntime(25929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sensordatacollector.smartrac/com.sensordatacollector.smartrac.Route}: java.lang.NullPointerException
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.ActivityThread.access$600(ActivityThread.java:151)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.os.Looper.loop(Looper.java:155)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.ActivityThread.main(ActivityThread.java:5536)
11-10 21:15:14.027: E/AndroidRuntime(25929): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 21:15:14.027: E/AndroidRuntime(25929): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 21:15:14.027: E/AndroidRuntime(25929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
11-10 21:15:14.027: E/AndroidRuntime(25929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
11-10 21:15:14.027: E/AndroidRuntime(25929): at dalvik.system.NativeStart.main(Native Method)
11-10 21:15:14.027: E/AndroidRuntime(25929): Caused by: java.lang.NullPointerException
11-10 21:15:14.027: E/AndroidRuntime(25929): at com.sensordatacollector.smartrac.Route.onCreate(Route.java:36)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.Activity.performCreate(Activity.java:5066)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
11-10 21:15:14.027: E/AndroidRuntime(25929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
11-10 21:15:14.027: E/AndroidRuntime(25929): ... 11 more
4

2 回答 2

0

发现我的错误。

double s_latitude = latitude.get(0);
double s_longitude=longitude.get(0);

longitude = (ArrayList<Double>) getIntent().getSerializableExtra("Longitude");
latitude = (ArrayList<Double>) getIntent().getSerializableExtra("Latitude");

它应该是另一个顺序。

longitude = (ArrayList<Double>) getIntent().getSerializableExtra("Longitude");
latitude = (ArrayList<Double>) getIntent().getSerializableExtra("Latitude");

double s_latitude = latitude.get(0);
double s_longitude=longitude.get(0);

愚蠢的错误!多谢你们 !

于 2013-11-11T03:31:47.003 回答
0

试试这个:

private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
        .position(new LatLng(s_latitude,s_longitude))
        .title("Start point"));
于 2013-11-11T05:03:42.097 回答