我是android编程的新手。我正在创建一个项目,该项目需要计算两个地理位置之间的路线。我的点和谷歌地图都正确,但是当我调用GeoApiContext时,我的程序崩溃了。这是错误:
D/DockFinderActivity: calculateDirections: calculating directions.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anchor.demo, PID: 21685
java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.java:449)
at com.google.maps.OkHttpRequestHandler$Builder.<init>(OkHttpRequestHandler.java:106)
at com.google.maps.GeoApiContext$Builder.<init>(GeoApiContext.java:318)
at com.example.anchor404.DockFinderActivity.calculateDirections(DockFinderActivity.java:497)
at com.example.anchor404.DockFinderActivity.access$900(DockFinderActivity.java:65)
at com.example.anchor404.DockFinderActivity$8.onClick(DockFinderActivity.java:473)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.immutableList(Util.java:234)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.java:449)
at com.google.maps.OkHttpRequestHandler$Builder.<init>(OkHttpRequestHandler.java:106)
at com.google.maps.GeoApiContext$Builder.<init>(GeoApiContext.java:318)
at com.example.anchor404.DockFinderActivity.calculateDirections(DockFinderActivity.java:497)
at com.example.anchor404.DockFinderActivity.access$900(DockFinderActivity.java:65)
at com.example.anchor404.DockFinderActivity$8.onClick(DockFinderActivity.java:473)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 21685 SIG: 9
Process 21685 terminated.
我这样称呼geoApiContext:
if(mGeoApiContext == null){
mGeoApiContext = new GeoApiContext.Builder()
.apiKey(getString(R.string.google_map_api_key))
.build();
}
mGeoApiContext是全局的:
private GeoApiContext mGeoApiContext = null;
在我的gradle中:
// Google Maps Services (needed for directions)
implementation 'com.google.maps:google-maps-services:0.10.1'
implementation 'org.slf4j:slf4j-simple:1.7.25'
我的清单具有以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
我是否缺少许可?我打错GeoApiContext还是API密钥有问题?
最佳答案
我在测试您的代码时遇到了相同的错误,因此经过大量研究,我发现this issue。您需要将源兼容性和目标兼容性都设置为1.8(Java 8)。
build.gradle
android {
compileOptions {
targetCompatibility = 1.8
sourceCompatibility = 1.8
}
}
或者,如果您使用的是Android Studio,请转到
File > Project Structure > Modules > Properties tab
。完成上述更改后,我可以在我的Android应用程序中毫无问题地使用
GeoApiContext
了,希望对您有所帮助!