最近要做一个项目,需要使用地图组件,于是准备使用第三方百度地图SDK来实现。
xml文件中
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.baidu.mapapi.map.MapView
android:id="@+id/baiduMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>
但是每次启动app时,总是闪退,报错如下
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.together.testmap10, PID: 32293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.together.testmap10/com.together.testmap10.MainActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.baidu.mapapi.map.MapView
原因就是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SDK初始化必须在setContentView前面执行
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
}
SDK初始化(SDKInitializer.initialize(getApplicationContext());)必须在setContentView前面执行
版权声明:本文为m0_46491185原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。