2017-10-11 62 views
1

我想谷歌地图整合到Xamarin的Android。但是,收到错误,如标题中所写。此错误出现在我的SetContentView(Resource.Layout.Main);如下图所示,其中:未处理的异常:Android.Views.InflateException:二进制XML文件行#1:二进制XML文件行#1:错误充气类片段发生

Error occurred in MainActivity.cs

我的谷歌播放服务的地图是了最新。我已经尝试清理和重建项目,也重新启动VS没有为我工作。非论坛回答我的问题。

这些是写在我的项目为:

MainActivity.cs

using System; 

using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Android.Gms.Maps; 

namespace Google_maps.Droid 
{ 
    [Activity (Label = "@string/Google_maps.Android", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity, IOnMapReadyCallback 
    { 
     private GoogleMap mMap; 

     protected override void OnCreate (Bundle bundle) 
     { 
      base.OnCreate (bundle); 

      // Set our view from the "main" layout resource 
      SetContentView (Resource.Layout.Main); 
      SetUpMap(); 

     } 

     private void SetUpMap() 
     { 
      if (mMap == null) 
      { 
       FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this); 
      } 
     } 

     public void OnMapReady(GoogleMap googleMap) 
     { 
      mMap = googleMap; 
     } 

    } 
} 

Main.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:name="com.google.android.gms.maps.MapFragment"/> 



</LinearLayout> 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, Click Me!</string> 
    <string name="app_name">Google_maps.Android</string> 
    <string name="google_maps_key">my_API_key</string> 
    <string name="Google_maps.Android">Google maps</string> 
</resources> 

最后,

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Google_maps.google_mapsAndroid" android:installLocation="preferExternal"> 
    <uses-sdk android:minSdkVersion="15" /> 
    <application android:label="Google_maps.Android" android:icon="@drawable/Icon"> 
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
    <meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="@string/google_maps_key" /> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
</manifest> 

有关其他信息,我也跟着从YouTube用户,乔岩,在那里,他教我们的方法如何设置谷歌地图。这里是链接到他的视频:

https://www.youtube.com/watch?v=W6Q0olRPsus

如果你往下看注释部分,你可以看到很多人有同样的问题太多。此视频2年前写了,所以我会明白为什么它不会在这一现状的工作。不过,如果你们可以启发我将Google地图集成到我的Android移动应用程序的正确方法,我将不胜感激,如果以某种方式,这种方法不再可靠。先谢谢你。

问候, Xamarin和编程新手。

编辑:

当我拖我的鼠标SetContentView (Resource.layout.Main)它说无效Activity.SetContentView(INT LayoutResID)(+2重载) 设置一个布局资源的活动内容。

我应该对这个问题?

+0

通常,碎片不能嵌入到扩展活动的类中,而应该扩展FragmentActivity – Dinash

+0

然后我可以知道我该怎么做? –

+0

@Dinash完成,我已经使用Android.Support.V4.App'和另一个方法'Android.Support“导入了'public class MainActivity:Activity'到'public class MainActivity:FragmentActivity'并添加了相应的导入语句 – Dinash

回答

1

所以我的项目无法打开,在我不得不重新创建我的项目的情况下,从头开始。我对模拟器进行了更改,我认为这可能一直是问题所在,并设法使其正常工作并删除标题上的错误。

虽然目前和唯一的问题是,它只在左下角显示“谷歌”徽标,但没有地图。我应该写在另一个线程?既然现在是一个不同的案例。

但到目前为止,错误已被删除,我相信这些设备和Android版本在这方面扮演着重要角色。

编辑:

您好再次,我已经成功地获得谷歌地图完全工作在我的应用程序。正如我之前所说的,“设备和Android版本在开发应用程序中起着重要作用”。仅供参考,我使用的是Android 7.1版(Nougat),Nexus 5设备,Google API x86 - API 25。

  1. 集编译器8.0(奥利奥)在性能>>应用和目标,以“使用编译使用SDK版本”,在属性>> Android清单。

  2. 更新您的Google地图包由Xamarin公司从的NuGet包管理器,在工具最新的稳定版本发现。我目前最新的稳定版本是42.1021.1。

  3. 更新您的Xamarin.Android.Support.Compat通过Xamarin公司从NuGet包管理器,在工具发现的最新的稳定版本。我目前最新的稳定版本是26.0.2。如果片段库没有与此一起更新,只需手动更新。

  4. 我说可能缺少权限,如下图所示:

<permission android:name="googleMapsProject.googleMapsProject.droid.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> 
 
\t <uses-permission android:name="com.deg.blubcnmobl.droid.permission.MAPS_RECEIVE" />

FYI的googleMapsProject.googleMapsProject是你的包名。确保它不是以大写字母开头,并且与您在Google控制台的“关键限制”页面中输入的包名称相匹配。

  1. 所有这些都在google地图API的第2版上运行。

这几乎是我得到它的工作。希望这可以帮助!

0

尝试:

变化:

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:name="com.google.android.gms.maps.MapFragment"/> 

要:

<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.SupportMapFragment"/> 

此外,在您的SetUpMap()方法变更如下:

FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this); 

为:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
从的NuGet

2.使用AppCompatActivity而不是活动

0

1.安装Appcompact DLL

3.添加在您的AndroidManifest以下权限下的性能

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER"/> 
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 

4.增加下面的xml在你的main.axml中

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.MapFragment" /> 

5.Add在styles.xml下面的代码下的资源/价值/风格:

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
<item name="windowNoTitle">true</item> 
<item name="windowActionBar">false</item> 
<item name="colorPrimary">#FFCC01</item> 
<item name="colorPrimaryDark">#E69D02</item> 
<item name="colorControlHighlight">#FFCC01</item> 
<item name="colorAccent">#D5012A</item> 
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
</style> 

<style name="DrawerArrowStyle" 
    parent="@style/Widget.AppCompat.DrawerArrowToggle"> 
    <item name="spinBars">true</item> 
    <item name="color">@color/colorRedText</item> 
</style> 

<style name="MyTheme" parent="MyTheme.Base"> 
</style> 

6.Make我的主题,通过在AndroidManifest.xml文件中添加以下行的默认应用程序的主题,你可以在你的项目的属性下找到,在当前的Android项目下面。

<application android:label="YourAppName" android:theme="@style/MyTheme" android:hardwareAccelerated="true" android:largeHeap="true" android:icon="@drawable/YourAppIcon"> 

而如果你不明白什么,你可以简单地点击这里:

https://developer.xamarin.com/guides/android/platform_features/maps_and_location/

还要检查,如果你有谷歌在AndroidSdk发挥服务,你可以在VS-工具栏中找到15工具> Android> AndroidSdk 在附加功能下安装Google Play服务。

+0

谢谢@Ghakim的回应。我尝试过这种方法,但是在SetContentView(Resource.layout.Main)中抛出一个新错误' 错误: **未处理的异常: Java.Lang.IllegalStateException:您需要使用Theme.AppCompat主题(或后代)与此活动。发生**。 另外,当我将鼠标拖拽到'SetContentView(Resource.layout.Main)'时,它说** void Activity.SetContentView(int LayoutResID)(+2重载) 设置布局资源上的活动内容。** 我应该关注这个吗? –

+0

我已经更新了我的回答,看一看,没有发现错误,因为根据上面的代码更新后,如果没有默认主题,它将消失@BahAkif –

+0

我认为我们快到了。我正在经历一个错误,它出现在** styles.xml **中,显示错误; **找不到与给定名称匹配的资源(位于'drawerArrowStyle',值为'@ style/DrawerArrowStyle')**。 我使用了一个名为** Android Support Library v7 AppCompat **的组件,并通过** Xamarin.inc **安装了名为** Xamarin.Android.Support.v7.AppCompat **的块组件。我是否下载正确的软件包和组件? –

相关问题