Kotlin:getFusedLocationProviderClient 崩溃
Kotlin: getFusedLocationProviderClient crashes
我正在学习教程 from Raywenderlich on Google Maps with Kotlin。我有以下代码由于我无法理解的原因而崩溃:
MapActivity.kt
import com.google.android.gms.location.LocationServices
...
private lateinit var map: GoogleMap
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}
build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
我已经注释掉了 getFusedLocationProviderClient()
,应用运行正常。一旦取消注释,应用程序就会崩溃。我做错了什么吗?是否可能是对 gms:play-services-location:11.8.0
版本的引用可能是问题所在。如果是这样,我如何检查我安装的版本?
更新 这是来自Logcat
的冲突日志
--------- beginning of crash
2019-12-23 09:37:02.698 14916-14916/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.johndoe.cityguide, PID: 14916
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/api/Api$zzf;
at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0)
at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0)
at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.api.Api$zzf" on path: DexPathList[[zip file "/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/base.apk"],nativeLibraryDirectories=[/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0)
at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0)
at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我想我 运行 曾经遇到过类似的问题;这是由于 Play 服务依赖项之间的版本冲突。我目前使用以下方法来利用 FusedLocationProviderClient:
dependencies {
def play_services_version = '17.0.0'
...
implementation "com.google.android.gms:play-services-maps:$play_services_version"
implementation "com.google.android.gms:play-services-location:$play_services_version"
}
希望对您有所帮助!
我正在学习教程 from Raywenderlich on Google Maps with Kotlin。我有以下代码由于我无法理解的原因而崩溃:
MapActivity.kt
import com.google.android.gms.location.LocationServices
...
private lateinit var map: GoogleMap
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}
build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
我已经注释掉了 getFusedLocationProviderClient()
,应用运行正常。一旦取消注释,应用程序就会崩溃。我做错了什么吗?是否可能是对 gms:play-services-location:11.8.0
版本的引用可能是问题所在。如果是这样,我如何检查我安装的版本?
更新 这是来自Logcat
的冲突日志--------- beginning of crash
2019-12-23 09:37:02.698 14916-14916/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.johndoe.cityguide, PID: 14916
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/api/Api$zzf;
at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0)
at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0)
at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.api.Api$zzf" on path: DexPathList[[zip file "/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/base.apk"],nativeLibraryDirectories=[/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0)
at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0)
at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我想我 运行 曾经遇到过类似的问题;这是由于 Play 服务依赖项之间的版本冲突。我目前使用以下方法来利用 FusedLocationProviderClient:
dependencies {
def play_services_version = '17.0.0'
...
implementation "com.google.android.gms:play-services-maps:$play_services_version"
implementation "com.google.android.gms:play-services-location:$play_services_version"
}
希望对您有所帮助!