我是xamarin的新手,我需要在应用程序中添加谷歌地图。
我从xamarin下载了这个例子:
https://github.com/xamarin/monodroid-samples/tree/master/MapsAndLocationDemo_v2
经过一些更改之后,它运行了,这个项目显示了地图。
这是有效的项目代码:
/ MainActivity.cs /
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Gms.Common;
using Android.OS;
using Android.Util;
using Android.Views;
using Android.Widget;
using AndroidUri = Android.Net.Uri;
namespace SimpleMapDemo
{
[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
public static readonly int InstallGooglePlayServicesId = 1000;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.BasicDemo);
}
}
}
/ BasicDemo.axml /
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
/ 的AndroidManifest.xml /
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="4.0" package="com.xamarin.docs.android.mapsandlocationdemo2" android:versionCode="6">
<uses-sdk android:targetSdkVersion="14" android:minSdkVersion="14" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- These are optional, but recommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
<uses-permission android:name="com.xamarin.docs.android.mapsandlocationdemo2.permission.MAPS_RECEIVE" />
<permission android:name="com.xamarin.docs.android.mapsandlocationdemo2.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<application android:label="@string/app_name" android:icon="@drawable/icon">
<!-- Put your Google Maps V2 API Key here. This key will not work for you.-->
<!-- See https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key -->
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="apikeytest" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
问题是,当我尝试将此代码传递给我想要添加地图的现有项目时,在执行它时会抛出异常:
Android.Views.InflateException:二进制XML文件行#1:错误膨胀类片段
失败的代码:
/ SplashScreen.cs /
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cirrious.MvvmCross.Droid.Views;
using Android.Content.PM;
//using Android.Gms.Common;
using AndroidUri = Android.Net.Uri;
using Android.Util;
using Android.Gms.Common;
using Android.Support.V4.App;
using Android.Gms.Maps;
namespace ProjectWithMap
{
[Activity(
Label = "ProjectWithMap",
MainLauncher = true,
Icon = "@drawable/icon",
//Theme ="@style/Theme.Splash",
ScreenOrientation = ScreenOrientation.Portrait,
NoHistory = true)]
public class SplashScreen : Activity
{
public static readonly int InstallGooglePlayServicesId = 1000;
private MapFragment map;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.provaMapa);
}
}
}
/ provaMapa.axml /
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="ProjectWithMap.SplashScreen"
class="com.google.android.gms.maps.MapFragment" />
/ 的AndroidManifest.xml /
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="projectWithMap.projectWithMap" android:versionCode="6" android:versionName="4.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- These are optional, but recommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
<uses-permission android:name="projectWithMap.projectWithMap.permission.MAPS_RECEIVE" />
<permission android:name="projectWithMap.projectWithMap.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<application android:label="projectWithMap" android:icon="@drawable/Icon" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<!-- Google Maps V2 API Key -->
<!-- Para más documentación mirar https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key -->
<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="key" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
这段代码抛出了我的例外:
Android.Views.InflateException:二进制XML文件行#1:错误膨胀类片段
我不明白为什么在一个项目中工作而在另一个项目中没有,我试图搜索这个错误并找到了多个解决方案,尝试了所有我喜欢的没有好结果......
更改com.google.android.gms.maps.SupportMapFragment的com.google.android.gms.maps.MapFragment
更改公共类SplashScreen:公共类的活动SplashScreen:FragmentActivity
但没有任何效果,总是一样的错误。
我缺少什么?为什么它在一个项目中而不在另一个项目中工作?...
修改
似乎错误在清单中,当我执行时,在应用程序输出处:
<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="yourKey" />
喜欢它获得谷歌服务的清单......
我加了钥匙!
<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="key" />
应用程序的清单,应用程序的清单,Google服务的清单是什么?
修改
最后是xamarin或visual studio的错误,删除清单并再次创建它解决了问题。
答案 0 :(得分:1)
添加额外的工具
“ Google Play服务”
在Android Sdk Manager中...并且要注意只有一个标签“ application”(拼写正确)
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/mapkey"/>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
</application>
答案 1 :(得分:-1)
向清单文件添加<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
权限。