我实现了华为地图,遵循@shirley here解释的所有协议。地图第一次运行良好。但是在第二次加载时,它会创建一个奇怪的数组超出绑定异常,并在 setContent 视图上崩溃。
并在删除权限并再次允许用户权限后,它的工作正常。
这是我对地图片段的 oncreate。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MapsInitializer.setApiKey("---------")
setFullscreen(true)
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY)
}
gettingDatForMap()
}
并在视图中创建
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//hide bottombar
((requireActivity()) as MainActivity).hideShowBottomBar(true)
initialising()
gettingDatForMap()
getDeviceLocation()
car_maps.getMapAsync(this)
car_maps.onCreate(savedInstanceState) //throwing error crash here
implementingClickListener()
}
地图准备功能是这样的
override fun onMapReady(huaweiMap: HuaweiMap?) {
mMap = huaweiMap
mMap!!.addPolyline(
PolylineOptions().addAll(latLngList)
.color(ContextCompat.getColor(requireContext(), R.color.color_lightblackhome))
.width(1f)
)
if (ActivityCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_BACKGROUND_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
mMap!!.isMyLocationEnabled = true
mMap!!.uiSettings.isCompassEnabled = false
mMap!!.setOnMyLocationButtonClickListener(this)
// mMap!!.setOnMyLocationChangeListener(this)
mMap!!.setOnCameraMoveListener {
selectLocationFromMarker = mMap!!.cameraPosition.target
}
mMap!!.setOnCameraMoveStartedListener { i: Int ->
//perform action on camera moving
}
}
最后记录 cat 它抛出的错误
2021-08-25 15:39:54.545 19081-19081/com.shazza.shazzaapp E/HmsMapKit_CrashHandler_137:
deviceModel is MED-LX9 emuiVersion is EmotionUI_10.1.0 packageName is com.shazza.shazzaapp rom version is MED-L29 10.1.0.219(C185E4R2P2)
2021-08-25 15:39:54.552 19081-19081/com.shazza.shazzaapp E/HmsMapKit_ErrorTraceLogPusher_145: cache error trace log : ErrorTraceLogDTO{ scenario = UNCAUGHT_EXCEPTION', message='java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.huawei.hms.maps.provider.util.mah.b(Unknown Source:46)
at com.huawei.hms.maps.provider.util.mah.a(Unknown Source:74)
at com.huawei.hms.maps.provider.util.mag.a(Unknown Source:37)
at com.huawei.hms.maps.provider.huawei.MapViewImpl.a(Unknown Source:15)
at com.huawei.hms.maps.provider.huawei.MapViewImpl.<init>(Unknown Source:8)
at com.huawei.hms.maps.provider.huawei.MapViewImpl.<init>(Unknown Source:1)
at com.huawei.hms.maps.CreatorImpl.newMapViewDelegate(Unknown Source:11)
at com.huawei.hms.maps.internal.mac$maa.onTransact(Unknown Source:360)
at android.os.Binder.transact(Binder.java:921)
at com.huawei.hms.maps.mac.a(Unknown Source:28)
at com.huawei.hms.maps.internal.mad$maa$maa.b(Unknown Source:13)
at com.huawei.hms.maps.MapView$maa.a(Unknown Source:4)
at com.huawei.hms.maps.MapView$maa.createDelegate(Unknown Source:131)
at com.huawei.hms.feature.dynamic.DeferredLifecycleHelper.a(Unknown Source:44)
at com.huawei.hms.feature.dynamic.DeferredLifecycleHelper.onCreate(Unknown Source:5)
at com.huawei.hms.maps.MapView.onCreate(Unknown Source:40)
at com.shazza.shazzaapp.ui.userside.fragments.findcarmapsfragment.FindCarMapsFragment.onViewCreated(FindCarMapsFragment.kt:119)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
'}
在这个视图上工作正常
<fragment
android:layout_above="@+id/bottom_relative"
android:id="@+id/testFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在抛出错误时,
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_relative"
app:defaultNavHost="true"
android:visibility="visible"
app:navGraph="@navigation/main_navigation_graph" />