jasonflaherty
jasonflaherty

Reputation: 1944

NPE on Android gMap v2 Camera Animate

I get a strange error on one of my activities running a simple camera update for the user location.

Here is the log cat error info:

12-23 21:44:11.075: V/LAT/LON(3373): Lat:40.28031 | Lon:-120.50508
12-23 21:44:11.075: W/System.err(3373): java.lang.NullPointerException
12-23 21:44:11.075: W/System.err(3373):     at com.jasonflaherty.snoteldata.NOAASnowDepthSites.animateCameraTo(NOAASnowDepthSites.java:654)
12-23 21:44:11.075: W/System.err(3373):     at com.jasonflaherty.snoteldata.NOAASnowDepthSites.showLocation(NOAASnowDepthSites.java:644)
12-23 21:44:11.075: W/System.err(3373):     at com.jasonflaherty.snoteldata.NOAASnowDepthSites.onCreate(NOAASnowDepthSites.java:106)
12-23 21:44:11.075: W/System.err(3373):     at android.app.Activity.performCreate(Activity.java:5231)
12-23 21:44:11.075: W/System.err(3373):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-23 21:44:11.075: W/System.err(3373):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-23 21:44:11.075: W/System.err(3373):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-23 21:44:11.075: W/System.err(3373):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-23 21:44:11.075: W/System.err(3373):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-23 21:44:11.075: W/System.err(3373):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 21:44:11.075: W/System.err(3373):     at android.os.Looper.loop(Looper.java:136)
12-23 21:44:11.075: W/System.err(3373):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-23 21:44:11.075: W/System.err(3373):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 21:44:11.075: W/System.err(3373):     at java.lang.reflect.Method.invoke(Method.java:515)
12-23 21:44:11.083: W/System.err(3373):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-23 21:44:11.083: W/System.err(3373):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-23 21:44:11.083: W/System.err(3373):     at dalvik.system.NativeStart.main(Native Method)
12-23 21:44:11.137: W/ActivityThread(3373): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
12-23 21:44:11.145: W/ResourceType(3373): getEntry failing because entryIndex 13 is beyond type entryCount 1
12-23 21:44:11.145: W/ResourceType(3373): Failure getting entry for 0x7f0b000d (t=10 e=13) in package 0 (error -2147483647)

As you can see, the app returns the lat/lon, but then I get the error on animate or there about... Here is my showLocation() code:

float lat = new LocationInfo(getBaseContext()).lastLat;
        float lon = new LocationInfo(getBaseContext()).lastLong;

        Log.v("LAT/LON", "Lat:" + lat + " | Lon:" + lon);

        try {
            LatLng meLoc = new LatLng(lat, lon);
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(meLoc) // Sets the center of the map to
                    .zoom(ZP).bearing(0).tilt(80).build();
            map.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

It specifically fails at:

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

I got that from: https://developers.google.com/maps/documentation/android/views#updating_the_camera_view

Anyhow... Any ideas?! I'm confused.

Upvotes: 0

Views: 117

Answers (1)

GLee
GLee

Reputation: 5093

Where are you calling this code? If it's in onCreate(), the map isn't instantiated yet (until after onCreateView). Try updating the camera position in onStart() or onResume().

Upvotes: 2

Related Questions