尝试使用百度地图获取位置时应用程序崩溃

App crash when try to get location with baidu map

我尝试在我的应用程序中实现百度地图。我设置了一切,它可以显示静态地图。但是当我尝试获取当前位置时,我的应用程序在我尝试调用方法 mLocationClient.start() 后崩溃。我已经设置了 api 键,请求权限,启用服务并将库放在正确的位置。但这次崩溃仍然存在。而且我无法从我的 Logcat 中获取任何崩溃信息。任何人都有在您的 android 应用程序中实现百度地图的经验吗?

import android.os.Bundle``
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.starkey.android.newlink.R

class BaiduMapDemoActivity : AppCompatActivity() {

    private var mMapView: MapView? = null
    private var findLocation: Button? = null
    lateinit var map: BaiduMap
    private var isFirstLoc = true

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        SDKInitializer.initialize(this)
        SDKInitializer.setCoordType(CoordType.BD09LL)

        setContentView(R.layout.baidu_demo)
        mMapView = findViewById(R.id.bmapView)
        findLocation = findViewById(R.id.addfence) as Button

        map = mMapView!!.map
        map.isMyLocationEnabled = true
        map.isMyLocationEnabled = true

        val mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING
        val mCurrentMarker = 
        BitmapDescriptorFactory.fromResource(R.drawable.ha_icon)
        val accuracyCircleFillColor = 0xAAFFFF88.toInt()
        val accuracyCircleStrokeColor = 0xAA00FF00.toInt()

        map.setMyLocationConfiguration(MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker,
                accuracyCircleFillColor, accuracyCircleStrokeColor))
        val mLocationClient = LocationClient(applicationContext)
        val mylistener = MyLocationListenner()

        mLocationClient.registerLocationListener(mylistener)
        val option = LocationClientOption()
        option.locationMode = 
(LocationClientOption.LocationMode.Hight_Accuracy)
        option.setCoorType("bd09ll")
        option.setScanSpan(1000)
        option.openGps = true
        option.setLocationNotify(true)
        option.setIgnoreKillProcess(false)
        option.SetIgnoreCacheException(false)
        option.setWifiCacheTimeOut(5 * 60 * 1000)
        option.setEnableSimulateGps(false)

        mLocationClient.setLocOption(option)

        (findLocation as Button).setOnClickListener {
            mLocationClient.start()
        }

        var test = mLocationClient.isStarted
        mLocationClient.requestLocation()
    }

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

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

    }

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

    inner class MyLocationListenner : BDAbstractLocationListener() {

        override fun onReceiveLocation(location: BDLocation?) {
            if (location == null || mMapView == null)
                return;
            val locData = MyLocationData.Builder()
                    .accuracy(location.radius)
                    .direction(100.toFloat()).latitude(location.latitude)
                    .longitude(location.longitude).build()
            map.setMyLocationData(locData)
            if (isFirstLoc) {
                isFirstLoc = false
                val ll = LatLng(location.latitude,
                        location.longitude)
                val u = MapStatusUpdateFactory.newLatLng(ll)
                map.animateMapStatus(u)
            }
        }
        fun onReceivePoi(poiLocation: BDLocation) {
        }
    }

来自Logcat的消息:

2019-01-04 13:37:57.483 25394-25394/com.starkey.android.newlink.debug I/System.out: Before call start

2019-01-04 13:37:59.255 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_BOOLEAN local from register v6 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])

2019-01-04 13:37:59.296 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_BOOLEAN local from register v6 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])

2019-01-04 13:37:59.299 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_INT local from register v9 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])

2019-01-04 13:37:59.305 25394-25401/com.starkey.android.newlink.debug E/zygote64: Failed to read JT_BOOLEAN local from register v12 at DEX pc 0x0000014f in method void com.android.internal.os.ZygoteInit.main(java.lang.String[])

2019-01-04 13:39:08.749 25394-25394/com.starkey.android.newlink.debug I/System.out: after call start

AFAIK,SDKInitializer.initialize 应该使用 ApplicationContextActivity 里面的 this 是不够的。

使用

SDKInitializer.initialize(applicationContext)

而不是

SDKInitializer.initialize(this)

参考:Hello BaiduMap(中文)

  1. The referenced Context used for initialize SDK during application creation is a global variable;

Warning: Before using any components in SDK, (you) must call SDKInitializer.initialize(getApplicationContext());. So ee suggest to put this method inside the initialization method of Application.

请从 libs 和 jin Libs 文件夹中删除 .jar 和 .so 文件,并在 app.gragle

中的 api 下面添加
implementation 'com.baidu.mapapi:base:3.7.1'
implementation 'com.baidu.mapapi:map:3.7.1'
implementation 'com.baidu.mapapi:search:3.7.1'
implementation 'com.baidu.mapapi:util:3.7.1'

现在,修复了 SDKInitializer.initialize(applicationContext) 崩溃问题。