我正在尝试实现 DirectionsApi,以便在我的 Android 应用程序中使用 Kotlin 在 Google 地图上绘制折线。我使用以下方法添加了所需的库:
implementation 'com.google.maps:google-maps-services:0.9.0'
然后在我的MapFragment中包含以下函数:
private fun getGeoContext() : GeoApiContext {
val geoApiContext = GeoApiContext.Builder()
.apiKey(getString(R.string.directions_apikey))
.build()
return geoApiContext
}
最后,在我的 onMapReady 中,我尝试使用以下方法获取方向结果:
val origin = "Hotel Hillview Munnar, State Highway 16, Moolakadai, Munnar, Kerala, India"
val dest = "Echo Point, Kannan Devan Hills, Kerala, India"
val apiRequest = DirectionsApi.newRequest(getGeoContext())
apiRequest.origin(origin)
apiRequest.destination(dest)
apiRequest.mode(TravelMode.DRIVING)
apiRequest.setCallback(object : com.google.maps.PendingResult.Callback<DirectionsResult> {
override fun onResult(result:DirectionsResult) {
Timber.i("Call Success")
val routes = result.routes
}
override fun onFailure(e:Throwable) {
}
})
但它一直在失败。我检查了 Logcat 日志,发现这是错误:
10-01 17:22:41.248 4112-4193/com.acme.internal E/AndroidRuntime: FATAL EXCEPTION: Rate Limited Dispatcher
Process: com.acme.internal, PID: 4112
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDateTime;
at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241)
at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.LocalDateTime" on path: DexPathList[[zip file "/data/app/com.acme.internal-1/base.apk"],nativeLibraryDirectories=[/data/app/com.acme.internal-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241)
at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Suppressed: java.lang.ClassNotFoundException: java.time.LocalDateTime
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 8 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
10-01 17:22:41.248 4112-4193/com.acme.internal E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
我猜这与使用 LocalDateTime 类的 Google 地图库的一些问题有关,但由于我使用的是 kotlin,它无法访问该类?这个问题有什么解决方法吗?
我现在自己想出了一个解决方案。这不是最好的解决方案,但解决了手头的问题。写在这里,所以它可以帮助其他任何人寻找同一问题的解决方案。
将 gradle 依赖项从:
implementation 'com.google.maps:google-maps-services:0.9.0'
自
implementation 'com.google.maps:google-maps-services:0.2.11'
它应该修复它。