Android gMap v2相机动画上的NPE

时间:2013-12-24 05:56:15

标签: android google-maps-android-api-2

我的一项活动在为用户位置运行简单的相机更新时出现了一个奇怪的错误。

以下是日志cat错误信息:

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)

正如你所看到的,应用程序返回了lat / lon,但后来我得到了关于animate的错误或者有关...这是我的showLocation()代码:

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

它特别失败于:

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

我从https://developers.google.com/maps/documentation/android/views#updating_the_camera_view

那里得到了

无论如何......有什么想法?!我很困惑。

1 个答案:

答案 0 :(得分:2)

你在哪里调用这段代码?如果它在onCreate()中,则地图尚未实例化(直到onCreateView之后)。尝试在onStart()或onResume()中更新相机位置。