百度地图搭建出现的问题,望赐教!
使用的是百度Android SDKv2.1.3(矢量地图版)学习开发,开始按照官方的开发指南,后来按照老罗的视频学习,但是api升级了,结合着老罗的视频和官方开发指南试着学习,最后模拟器运行出现了问题,希望大家帮帮我这个初学者,谢谢!最终运行效果图:
下面附上配置代码和java源码:
AndroidManifest.xml配置代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.mybaidu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 对屏幕支持 -->
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />
<!-- 添加百度地图开发授权 -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<!-- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" >
</uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 来电消音 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.mybaidu.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapDemo"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="sensor" >
</activity>
</application>
</manifest>
layout中activity_main.xml的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- 添加百度地图控件 -->
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</RelativeLayout>
MainActivity.java的源代码
package com.android.mybaidu;--------------------编程问答-------------------- 错误贴出来日志 --------------------编程问答-------------------- 帮顶吧 等高手
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity{
//添加百度地图的相关控件
private MapView mapView;
//百度地图的引擎
private BMapManager bMapManager;
//百度地图的密钥
private String keyString="0F1e1032908159a78100d79622a5aec5";
//在地图上添加一些控件
private MapController mapController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView=(MapView)this.findViewById(R.id.action_bmapView);
bMapManager=new BMapManager(MainActivity.this);
//必须要加载key
bMapManager.init(keyString, new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
// TODO Auto-generated method stub
if (arg0==300) {
Toast.makeText(MainActivity.this, "输入的key有误,请核实!!!", 1).show();
}
}
@Override
public void onGetNetworkState(int arg0) {
// TODO Auto-generated method stub
}
});
//this.initMapActivity(bMapManager);
mapView.setBuiltInZoomControls(true);
GeoPoint point =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));
mapController.setCenter(point);//设置地图中心点
mapController.setZoom(12);//设置地图zoom级别
}
// @Override
// protected void onDestroy() {
// // TODO Auto-generated method stub
// super.onDestroy();
// if (bMapManager !=null) {
// bMapManager.destroy();
// bMapManager=null;
// }
// }
@Override
protected void onDestroy(){
mapView.destroy();
if (bMapManager !=null) {
bMapManager.destroy();
bMapManager=null;
}
super.onDestroy();
}
// @Override
// protected void onResume() {
// // TODO Auto-generated method stub
// super.onResume();
// if (bMapManager!=null) {
// bMapManager.start();
// }
// }
@Override
protected void onResume(){
mapView.onResume();
if(bMapManager!=null){
bMapManager.start();
}
super.onResume();
}
// @Override
// protected void onPause() {
// // TODO Auto-generated method stub
// super.onPause();
// if (bMapManager!=null) {
// bMapManager.stop();
// }
// }
@Override
protected void onPause(){
mapView.onPause();
if (bMapManager!=null) {
bMapManager.stop();
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我之前也试过一次,也是打开就出错了,看log是加载baidu的so失败,缺少JNI_Load。。。 --------------------编程问答-------------------- 百度地图所需要的jar和so都放到工程里面了么? --------------------编程问答-------------------- 附上logcat捕获的error代码:
09-01 10:56:43.598: E/AndroidRuntime(835): FATAL EXCEPTION: main--------------------编程问答-------------------- : Binary XML file line #13: Error inflating class com.baidu.mapapi.map.MapV
09-01 10:56:43.598: E/AndroidRuntime(835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mybaidu/com.android.mybaidu.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.baidu.mapapi.map.MapView
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.os.Looper.loop(Looper.java:137)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-01 10:56:43.598: E/AndroidRuntime(835): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 10:56:43.598: E/AndroidRuntime(835): at java.lang.reflect.Method.invoke(Method.java:525)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-01 10:56:43.598: E/AndroidRuntime(835): at dalvik.system.NativeStart.main(Native Method)
09-01 10:56:43.598: E/AndroidRuntime(835): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.baidu.mapapi.map.MapView
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.Activity.setContentView(Activity.java:1895)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.android.mybaidu.MainActivity.onCreate(MainActivity.java:26)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.Activity.performCreate(Activity.java:5133)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-01 10:56:43.598: E/AndroidRuntime(835): ... 11 more
09-01 10:56:43.598: E/AndroidRuntime(835): Caused by: java.lang.reflect.InvocationTargetException
09-01 10:56:43.598: E/AndroidRuntime(835): at java.lang.reflect.Constructor.constructNative(Native Method)
09-01 10:56:43.598: E/AndroidRuntime(835): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-01 10:56:43.598: E/AndroidRuntime(835): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
09-01 10:56:43.598: E/AndroidRuntime(835): ... 22 more
09-01 10:56:43.598: E/AndroidRuntime(835): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: com.baidu.platform.comjni.map.basemap.JNIBaseMap.Create:()I
09-01 10:56:43.598: E/AndroidRuntime(835): at com.baidu.platform.comjni.map.basemap.JNIBaseMap.Create(Native Method)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.baidu.platform.comjni.map.basemap.a.a(Unknown Source)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.baidu.platform.comapi.map.o.a(Unknown Source)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.baidu.platform.comapi.map.q.a(Unknown Source)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.baidu.mapapi.map.MapView.a(Unknown Source)
09-01 10:56:43.598: E/AndroidRuntime(835): at com.baidu.mapapi.map.MapView.<init>(Unknown Source)
09-01 10:56:43.598: E/AndroidRuntime(835): ... 25 more
说的很清楚了啊,13行的xml错误 --------------------编程问答-------------------- 也遇到同样的问题。差点被折磨疯,后来发现是自己的错误。官方demo里面有一个 继承application的类。你需要在manifest.xml文件里进行声明,如何声明,比对下demo即可。然后问题就解决了。 --------------------编程问答-------------------- Caused by: java.lang.UnsatisfiedLinkError:
库没有加载上。有没有把库放到system/lib目录下? --------------------编程问答--------------------
非常赞成该楼的说法,代码什么的和百度demo都一样,包引用全部正确,initManager之后才setContentView,如果还有错,就是manifest里的application中的android:name=".DemoApp"把对应的App加上即可. --------------------编程问答-------------------- tools:context=".MainActivity"
xml 中去掉它就好了
补充:移动开发 , Android