首页 > 解决方案 > MapBox 9.2.0 在 Android 8.0 (Oreo) JNI DETECTED ERROR IN APPLICATION 中导致崩溃

问题描述

没有问题,但是当它在 3 或 5 分钟后进入后台时,应用程序崩溃 JNI DETECTED ERROR IN APPLICATION: can't call java.lang.Object java.lang.ref.Reference.get() on instance java.lang.String 从 void android.os.MessageQueue.nativePollOnce(long, int) 调用 CallObjectMethodV,这就是它在日志中显示的内容。

class HomeFragment : BaseFragment() {

@Inject
lateinit var factory: ViewModelProvider.Factory
lateinit var binding: HomeBinding
var anchorClicked: Boolean = false
private lateinit var mapboxMap: MapboxMap


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding = inflater.bind(R.layout.fragment_home, container)
    return binding.root
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    binding.handler = this
    binding.lifecycleOwner = viewLifecycleOwner
    binding.mapView.onCreate(savedInstanceState)
    val navigationView: NavigationView = requireActivity().findViewById(R.id.sideNV)
    val headerView = navigationView.getHeaderView(0)
    withViewModel<MapViewModel>(factory) {
        binding.mapView.getMapAsync { mapBoxMap ->
            this@HomeFragment.mapboxMap = mapBoxMap
            if (getCurrentTheme())
                mapBoxMap.setStyle(Style.TRAFFIC_NIGHT){
                    requestPermission(it)
                }
            else mapBoxMap.setStyle(Style.TRAFFIC_DAY){
                requestPermission(it)
            }
        }
        val auth = FirebaseAuth.getInstance()
        if (auth.currentUser != null) {
            val auth1 = FirebaseAuth.getInstance()
            if (auth1.currentUser != null) {
                headerView.userNameTV.text = auth1.currentUser?.displayName
                headerView.userEmailTV.text = auth1.currentUser?.email.toString()
                Glide.with(requireActivity())
                    .load(auth1.currentUser?.photoUrl)
                    .apply(RequestOptions.bitmapTransform(RoundedCorners(14)))
                    .into(headerView.userPicIV)
            }
        }
        binding.vm = this
        getCountries().observe(viewLifecycleOwner, Observer { })
    }
}

fun requestPermission(style:Style) {
    if (!hasPermissions(context, permissions)) requestPermissions(permissions, RC_LOCATION)
    else enableLocationComponent(style)
}

private fun enableLocationComponent(loadedMapStyle: Style) {
    val customLocationComponentOptions = LocationComponentOptions.builder(context!!)
        .trackingGesturesManagement(true)
        .accuracyColor(ContextCompat.getColor(context!!, R.color.com_facebook_blue))
        .build()

    val locationComponentActivationOptions =
        LocationComponentActivationOptions.builder(context!!, loadedMapStyle)
            .locationComponentOptions(customLocationComponentOptions)
            .build()

    mapboxMap.locationComponent.apply {
        activateLocationComponent(locationComponentActivationOptions)
        isLocationComponentEnabled = true
        cameraMode = CameraMode.TRACKING
        renderMode = RenderMode.COMPASS
    }
}


override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == RC_LOCATION) {
        if (grantResults.isNotEmpty()
            && grantResults[0] == PackageManager.PERMISSION_GRANTED
        ) {
            enableLocationComponent(mapboxMap.style!!)
        }
    }
}

companion object {
    const val RC_LOCATION = 10
}

}

这是我的代码。这是奥利奥中的某种限制/地图框的问题。如果是这样,可以做些什么来避免这种情况。我们可以使用协程来解决这个问题吗?

标签: androidgenericsmapboxkotlin-coroutinesmapbox-android

解决方案


推荐阅读