androidmapshuawei-mobile-serviceshuawei-developershuawei-map-kit

Huawei maps throwing array out of bound exception on rendering


I implemented huawei maps, followed all the protocols explained by @shirley here. the maps is working fine for the first time. but while loading second time it creates a weird array out of bound exception with a crash on setContent view.

and after removing the permission and than allowing the user permission again its working fine.

this is my oncreate of the map fragment.

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    MapsInitializer.setApiKey("---------")

    setFullscreen(true)

    if (savedInstanceState != null) {
        mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY)
    }

    gettingDatForMap()

}

and on view created

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()


}

the on map ready funtion is like this

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
    }
}

lastly log cat for the error which it throws

 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)
'}

works fine on this view

<fragment
       android:layout_above="@+id/bottom_relative"
       android:id="@+id/testFragment"           
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

while throwing error on,

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

Solution

  • Update

    You can try the following code:

    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()
        }
    
    }
    

    You can use the latest map kit version 6.0.0.301 and try it again by referring to the following code of the demo:

    link1

    link2

    these code show how to draw the Polylines properly.