华为地图在渲染时抛出数组超出绑定异常

Huawei maps throwing array out of bound exception on rendering

我实现了华为地图,遵循@shirley 解释的所有协议。 地图第一次运行良好。但是在第二次加载时它会创建一个奇怪的数组越界异常并在 setContent 视图上崩溃。

在删除权限并再次允许用户权限后,它的工作正常。

这是我创建的地图片段。

 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.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" />

更新

您可以试试下面的代码:

class App : Application() {

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MapsInitializer.initialize(base)
    }

    override fun onCreate() {
        super.onCreate()
    }

}
class FragMap : Fragment(), OnMapReadyCallback {
    private val TAG = "MapDemo"
    private val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"
    val FRANCE = LatLng(47.893478, 2.334595)
    val FRANCE1 = LatLng(48.993478, 3.434595)
    val FRANCE2 = LatLng(48.693478, 2.134595)
    val FRANCE3 = LatLng(-248.793478, -2.334595)
    //Huawei map
    private var hMap: HuaweiMap? = null
    private var mMapView: MapView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        MapsInitializer.setApiKey("key") //if use the agconnect-services.json file ,may dont need this line
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_blank_map, container, false)
        //get mapview instance
        mMapView = view.findViewById(R.id.mapView)
        var mapViewBundle: Bundle? = null
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY)
        }
        mMapView?.onCreate(mapViewBundle)
        //get map instance
        mMapView?.getMapAsync(this)
        return view
    }

    override fun onMapReady(map: HuaweiMap) {
        //get map instance in a callback method
        Log.d(TAG, "onMapReady: ")
        hMap = map
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(FRANCE, 10f))
        addPolyline()
    }

    fun addPolyline() {
        if (null == hMap) {
            return
        }
        val lngs = arrayOf(FRANCE, FRANCE1, FRANCE2, FRANCE3)
        hMap!!.addPolyline(
            PolylineOptions().add(*lngs)
                .color(Color.BLUE)
                .width(3f)
        )
        hMap!!.setOnPolylineClickListener { Log.i(TAG, "onMapReady:onPolylineClick ") }
    }

    override fun onStart() {
        super.onStart()
        mMapView!!.onStart()
    }

    override fun onStop() {
        super.onStop()
        mMapView!!.onStop()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mMapView!!.onDestroy()
    }

    override fun onPause() {
        mMapView!!.onPause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mMapView!!.onResume()
    }

}

您可以使用最新的map kit version 6.0.0.301,参考下面的demo代码再试一下:

link1

link2

这些代码展示了如何正确绘制折线。