【发布时间】:2015-02-19 12:09:04
【问题描述】:
我是 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.MapFragment 更改为 com.google.android.gms.maps.SupportMapFragment
更改公共类 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" />
应用获取的清单是什么,应用的清单,谷歌服务的清单?
编辑:
最后是xamarin或者visual studio的bug,删除manifest再重新创建就解决了。
【问题讨论】:
-
您是否也在项目中定义/包含
SampleMapActivity类?演示项目使用这个here 来实现将包含片段的布局。 -
我没有定义/包含 SampleMapActivity,但是因为它显示了有效的代码,所以我没有在 MainActivity 中使用它:SetContentView (Resource.Layout.BasicDemo);直接,所以我从主要活动转到基本演示视图,这在测试项目中有效,但在实际项目中无效。
-
最后是xamarin或者visual studio的bug,删除manifest再重新创建就解决了。
-
如果有人想在 xamarin android 中集成谷歌地图,那么这将是一个有用的appliedcodelog.blogspot.in/2015/04/…
标签: google-maps android-fragments xamarin google-api