谷歌地图示例项目构建,但在Android 4.4.2 w Google API上停止



谷歌作为教程提供的实际示例Google Android Studio Project CurrentPlaceDetailsOnMap似乎在带有API 19和Google API的android 4.4.2 AVD中崩溃。

任何人都可以自己直接测试。在您首次在当前的Android Studio 3.1中打开该项目后,它会为您提供一个Android Gradle插件更新建议对话框,然后我运行该更新。接下来,它给了我一堆配置"编译"已经过时,并且已被我通过替换引用来修复的实现错误所取代。然后,我将 gradle.properties 文件中的GOOGLE_MAPS_API_KEY替换为我在 google 注册后收到的密钥。 然后我构建并运行该应用程序,然后崩溃并显示">不幸的是,当前地点详细信息已停止"错误。注意:该应用程序在我的 4.4 设备上运行,但在 AVD 中不起作用。

除了将编译更改为实现并添加我的谷歌 api 密钥外,我没有对原始教程代码进行任何修改。 我注意到如果我有它跳过行:

mapFragment.getMapAsync(this);

然后它会运行,但不会放大到正确的位置。我不明白logcat跟踪的输出,但也许它将有助于调试问题。 任何建议如何在 AVD 中继续测试它而不会崩溃?谢谢。

更新:我粗略地尝试遵循堆栈跟踪,看起来 AVD 在第 212 行崩溃:

new LatLng(mLastKnownLocation.getLatitude(),

这是 logcat 控制台的下部 1/2:

04-08 22:50:57.911 4820-4820/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x005d
04-08 22:50:57.911 4820-4820/com.example.currentplacedetailsonmap I/dalvikvm: Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method tm.a
04-08 22:50:57.911 4820-4820/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve virtual method 683: Landroid/content/Context;.isDeviceProtectedStorage ()Z
04-08 22:50:57.911 4820-4820/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x0009
04-08 22:50:57.921 4820-4820/com.example.currentplacedetailsonmap D/dalvikvm: GC_FOR_ALLOC freed 373K, 13% free 3258K/3740K, paused 2ms, total 2ms
04-08 22:50:57.921 4820-4820/com.example.currentplacedetailsonmap D/dalvikvm: GC_FOR_ALLOC freed 9K, 13% free 3316K/3796K, paused 1ms, total 1ms
GC_FOR_ALLOC freed 1K, 13% free 3413K/3896K, paused 1ms, total 1ms
04-08 22:50:57.921 4820-4820/com.example.currentplacedetailsonmap I/dalvikvm-heap: Grow heap (frag case) to 4.512MB for 1127532-byte allocation
04-08 22:50:57.931 4820-4829/com.example.currentplacedetailsonmap D/dalvikvm: GC_FOR_ALLOC freed <1K, 10% free 4514K/5000K, paused 3ms, total 3ms
04-08 22:50:57.941 4820-4820/com.example.currentplacedetailsonmap D/dalvikvm: GC_FOR_ALLOC freed 43K, 9% free 4711K/5168K, paused 2ms, total 2ms
04-08 22:50:57.941 4820-4820/com.example.currentplacedetailsonmap W/ContextImpl: Failed to ensure directory: /storage/sdcard/Android/data/com.example.currentplacedetailsonmap/cache
04-08 22:50:57.951 4820-4840/com.example.currentplacedetailsonmap I/dalvikvm: Could not find method android.os.PowerManager.isInteractive, referenced from method om.a
04-08 22:50:57.951 4820-4840/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve virtual method 1094: Landroid/os/PowerManager;.isInteractive ()Z
04-08 22:50:57.951 4820-4840/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x003b
04-08 22:50:57.951 4820-4840/com.example.currentplacedetailsonmap W/ActivityThread: 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());
04-08 22:50:57.961 4820-4820/com.example.currentplacedetailsonmap D/dalvikvm: GC_FOR_ALLOC freed 352K, 9% free 4992K/5452K, paused 1ms, total 2ms
04-08 22:50:58.001 4820-4820/com.example.currentplacedetailsonmap D/EGL_emulation: eglCreateContext: 0xb7978e70: maj 2 min 0 rcv 2
04-08 22:50:58.021 4820-4820/com.example.currentplacedetailsonmap D/EGL_emulation: eglMakeCurrent: 0xb7978e70: ver 2 0
04-08 22:50:58.021 4820-4820/com.example.currentplacedetailsonmap E/EGL_emulation: tid 4820: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)
04-08 22:50:58.021 4820-4820/com.example.currentplacedetailsonmap W/HardwareRenderer: Backbuffer cannot be preserved
04-08 22:50:58.021 4820-4820/com.example.currentplacedetailsonmap D/OpenGLRenderer: Enabling debug mode 0
04-08 22:50:58.031 4820-4840/com.example.currentplacedetailsonmap D/dalvikvm: GC_FOR_ALLOC freed 486K, 11% free 5225K/5820K, paused 2ms, total 2ms
04-08 22:50:58.041 4820-4820/com.example.currentplacedetailsonmap D/AndroidRuntime: Shutting down VM
04-08 22:50:58.041 4820-4820/com.example.currentplacedetailsonmap W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d25b20)
04-08 22:50:58.041 4820-4820/com.example.currentplacedetailsonmap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.currentplacedetailsonmap, PID: 4820
java.lang.NullPointerException
at com.example.currentplacedetailsonmap.MapsActivityCurrentPlace$2.onComplete(MapsActivityCurrentPlace.java:212)
at com.google.android.gms.tasks.zzf.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
Selected remote version of com.google.android.gms.googlecertificates, version >= 4
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap I/dalvikvm: Could not find method android.content.ContextWrapper.createCredentialProtectedStorageContext, referenced from method ar.createCredentialProtectedStorageContext
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve virtual method 102: Landroid/content/ContextWrapper;.createCredentialProtectedStorageContext ()Landroid/content/Context;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap I/dalvikvm: Could not find method android.content.ContextWrapper.createDeviceProtectedStorageContext, referenced from method ar.createDeviceProtectedStorageContext
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve virtual method 103: Landroid/content/ContextWrapper;.createDeviceProtectedStorageContext ()Landroid/content/Context;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap I/dalvikvm: Could not find method o.createCredentialProtectedStorageContext, referenced from method p.createCredentialProtectedStorageContext
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve virtual method 725: Lo;.createCredentialProtectedStorageContext ()Landroid/content/Context;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap I/dalvikvm: Could not find method o.createDeviceProtectedStorageContext, referenced from method p.createDeviceProtectedStorageContext
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve virtual method 726: Lo;.createDeviceProtectedStorageContext ()Landroid/content/Context;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_64_BIT_ABIS
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve static field 58 (SUPPORTED_64_BIT_ABIS) in Landroid/os/Build;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0012
DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_32_BIT_ABIS
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve static field 57 (SUPPORTED_32_BIT_ABIS) in Landroid/os/Build;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0021
DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_64_BIT_ABIS
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap W/dalvikvm: VFY: unable to resolve static field 58 (SUPPORTED_64_BIT_ABIS) in Landroid/os/Build;
04-08 22:50:59.971 4820-4866/com.example.currentplacedetailsonmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0008

到目前为止,从堆栈跟踪来看,它似乎由于 mLastKnownLocation 中的空值而崩溃。

我在原始的Google Maps教程MapsActivityCurrentPlace.java文件中添加了一行:

if(mLastKnownLocation == null) { return; }

以上

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));

以返回到地图而不放大(如果没有 mLastKnownLocation)。

当您单击地图十字准线时,您将获得更新的位置。

有趣的是,4.4.2 AVD 从未存储过以前的位置(可能在缓存中),这导致它崩溃。我没有在 AVD 上尝试过不同版本的 API,并猜测它可能会在其他版本上崩溃。

最新更新