我正在构建一个使用地图片段的android应用程序。最近我重新设计了我的应用程序,并将地图视图移动到一个片段。一切工作之前,我要求权限和manifest具有当前片段的父活动。
而我试图得到融合的位置提供程序如下:
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mFusedLocationClient =
LocationServices.getFusedLocationProviderClient(Activity())
}
我:
E/AndroidRuntime: FATAL EXCEPTION: main过程:android.billboapp。billbo, PID: 8859java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.content.Context android.content.Context. getapplicationcontext ()'android.content.ContextWrapper.getApplicationContext (ContextWrapper.java: 122)在com.google.android.gms.common.api.GoogleApi。(com.google.android.gms: play-services-base@@18.0.1:7)在com.google.android.gms.common.api.GoogleApi。(com.google.android.gms: play-services-base@@18.0.1:1)在com.google.android.gms.location.FusedLocationProviderClient。(com.google.android.gms: play-services-location@@20.0.0:1)com.google.android.gms.location.LocationServices.getFusedLocationProviderClient (com.google.android.gms play-services-location@@20.0.0:1):android.billboapp.billbo.fragments.bottom_nav.BillboardsMapFragment.onMapReady (BillboardsMapFragment.kt: 157)com.google.android.gms.maps.zzat.zzb (com.google.android.gms play-services-maps@@18.0.0:1):com.google.android.gms.maps.internal.zzaq.zza (com.google.android.gms play-services-maps@@18.0.0:5):com.google.android.gms.internal.maps.zzb.onTransact (com.google.android.gms play-services-maps@@18.0.0:3):android.os.Binder.transact (Binder.java: 1043)在em.bc(com.google.android.gms.dynamite_mapsdynamite@222413081@22.24.13 (150400-0):2)在com.google.maps.api.android.lib6.impl.bf.run(:com.google.android.gms.dynamite_mapsdynamite@222413081@22.24.13 (150400-0):1)android.os.Handler.handleCallback (Handler.java: 938)android.os.Handler.dispatchMessage (Handler.java: 99)android.os.Looper.loop (Looper.java: 223)android.app.ActivityThread.main (ActivityThread.java: 7697)在java.lang.reflect.Method。调用(本地方法)com.android.internal.os.RuntimeInit MethodAndArgsCaller.run美元(RuntimeInit.java: 592)com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 952)
my fragment layout is:
?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.bottom_nav.BillboardsMapFragment">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="50"
android:src="@drawable/billbo_first_logo"
android:contentDescription="@string/logo_on_map" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="UselessParent">
<fragment
android:id="@+id/googleMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
我改了:
LocationServices.getFusedLocationProviderClient(Activity())
:
LocationServices.getFusedLocationProviderClient(requireActivity())