我看到他们的GitHub page出现了类似的问题,但是在新版本修复了此问题后,它们已关闭。我已将其作为错误提出,但尚未得到答复。
当包含地图的片段打开时,该崩溃几乎总是发生(该片段包含一个viewpager,而mapbox的片段托管在该viewpager中。
这些是我正在使用的唯一实现:
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.2.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.5.0'
implementation 'com.mapbox.mapboxsdk:mapbox-sdk-services:4.3.0'
这是完整的地图片段:
class ImageMapViewFragment : Fragment(), PermissionsListener {
private lateinit var sharedViewModelForImage : SharedViewModelImage
private lateinit var mapView: MapView
private lateinit var map: MapboxMap
private lateinit var permissionsManager: PermissionsManager
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.let {
sharedViewModelForImage = ViewModelProviders.of(it).get(SharedViewModelImage::class.java)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Mapbox.getInstance(activity!!.applicationContext, getString(co.getdere.R.string.mapbox_access_token))
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(co.getdere.R.layout.fragment_image_map_view, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView = view.findViewById(co.getdere.R.id.mapView)
sharedViewModelForImage.sharedImageObject.observe(this, Observer {
it?.let {image ->
mapView.onCreate(savedInstanceState)
mapView.getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.LIGHT) { it ->
map = mapboxMap
if (PermissionsManager.areLocationPermissionsGranted(this.context)) {
// Get an instance of the component
val locationComponent = mapboxMap.locationComponent
mapboxMap.addMarker(
MarkerOptions()
.position(LatLng(image.location[0], image.location[1])))
val position = CameraPosition.Builder()
.target(LatLng(image.location[0], image.location[1]))
.zoom(10.0)
.build()
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position))
// Activate with options
if (ContextCompat.checkSelfPermission(
this.context!!,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
locationComponent.activateLocationComponent(this.context!!, mapboxMap.style!!)
}
// Enable to make component visible
locationComponent.isLocationComponentEnabled = true
// Set the component's render mode
locationComponent.renderMode = RenderMode.COMPASS
} else {
permissionsManager = PermissionsManager(this)
permissionsManager.requestLocationPermissions(activity)
}
}
}
}
}
)
}
override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) {
Toast.makeText(this.context, "Location needed to use map", Toast.LENGTH_LONG).show()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onStart() {
super.onStart()
mapView.onStart()
}
override fun onResume() {
super.onResume()
mapView.onResume()
}
override fun onPause() {
super.onPause()
mapView.onPause()
}
override fun onStop() {
super.onStop()
mapView.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
override fun onDestroyView() {
super.onDestroyView()
mapView.onDestroy()
}
companion object {
fun newInstance(): ImageMapViewFragment = ImageMapViewFragment()
}
}