【问题标题】:Unhandled Exception: Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class fragment occurred未处理的异常:Android.Views.InflateException:二进制 XML 文件第 1 行:二进制 XML 文件第 1 行:膨胀类片段时出错
【发布时间】:2017-10-11 02:45:32
【问题描述】:

我正在尝试将谷歌地图集成到 Xamarin Android。但是,收到错误,如标题中所写。这个错误出现在我的 SetContentView (Resource.Layout.Main); 如下所示:

Error occurred in MainActivity.cs

我的 google play 地图服务是最新的。我已经尝试清理和重建项目,也重新启动 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>

有关更多信息,我遵循了 youtuber Joe Rock 的方法,他教我们如何设置谷歌地图。这是他的视频链接:

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

如果您查看评论部分,您会发现许多其他人也遇到了同样的问题。该视频是 2 年前制作的,所以我可以理解为什么它在目前的情况下不起作用。但是,如果你们能启发我将谷歌地图集成到我的 android 移动应用程序的正确方法,我将不胜感激,如果不知何故,这种方法不再可靠。提前谢谢你。

问候, Xamarin 和编程新手。

编辑:

当我将鼠标拖到 SetContentView (Resource.layout.Main) 时,它会显示 void Activity.SetContentView(int LayoutResID)(+2 重载) 在布局资源上设置活动内容。

我应该担心这个吗?

【问题讨论】:

  • 一般来说,Fragment 不能嵌入到扩展 Activity 的类中,而是应该扩展 FragmentActivity
  • 那我可以知道我到底应该怎么做吗?
  • public class MainActivity : Activity 更改为 public class MainActivity : FragmentActivity 并添加适当的导入语句
  • @Dinash 完成,我已经导入了using Android.Support.V4.App,还有另一个方法Android.Support.V4.App.FragmentActivity, IOnMapReadyCallBack。收到与以前相同的错误; (未处理的异常:Android.Views.InflateException:二进制 XML 文件第 1 行:二进制 XML 文件第 1 行:发生错误膨胀类片段)
  • 你试图在什么 API 中运行你的代码?同时分享构建 gradle。

标签: android xml google-maps xamarin.android inflate-exception


【解决方案1】:

所以我的项目无法打开,如果我不得不从头开始重新创建我的项目。我已经对我的模拟器进行了更改,我认为这可能一直是问题所在,并设法让它工作并删除了标题上的错误。

虽然目前唯一的问题是,它只在左下角显示“google”标志,但没有地图。我可能应该在另一个线程中写这个?因为现在情况不同了。

但到目前为止,错误已被消除,我相信设备和 android 版本在其中发挥了重要作用。

编辑:

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

  1. 在属性 >> 应用程序中将编译器设置为 8.0 (Oreo),在属性 >> Android 清单中将目标设置为“使用 SDK 版本编译”。

  2. 将 Xamarin Inc. 的 GoogleMaps 包从 工具 中的 NuGet 包管理器 更新到 最新的稳定版本。我目前最新的稳定版本是 42.1021.1。

  3. 将 Xamarin Inc. 的 Xamarin.Android.Support.Compat 从 工具 中的 Nuget 包管理器 更新到 最新稳定版本。我目前最新的稳定版本是 26.0.2。如果片段库没有随之更新,只需手动更新即可。

  4. 我添加了可能缺少的权限,如下所示:

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

仅供参考 googleMapsProject.googleMapsProject 是您的包名。确保它不以大写字母开头,并且与您在 google 控制台的 Key Restriction 页面中编写的包名称匹配。

  1. 所有这些都在谷歌地图 API v2 上运行。

这就是我让它工作的方式。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    试试:

    变化:

      <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);
    

    【讨论】:

      【解决方案3】:

      1.从 Nuget 安装 Appcompact dll

      2.使用AppCompatActivity代替Activity

      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.在你的 main.axml 中添加以下 xml

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

      5.在你的styles.xml中的resource/values/styles下添加如下代码:

      <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. 将我的主题设置为您的默认应用程序主题,方法是在您的 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 中是否有 Google Play 服务,您可以在 VS-15 工具>Android>AndroidSdk 的工具栏中找到这些服务 在 Extras 下安装 Google Play Services。

      【讨论】:

      • 感谢@G.hakim 的回复。我已经尝试过这种方法,但是在 SetContentView (Resource.layout.Main) 上引发了一个新错误错误:Unhandled Exception: Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this Activity。发生。此外,当我将鼠标拖动到 SetContentView (Resource.layout.Main) 时,它会显示 void Activity.SetContentView(int LayoutResID) (+2 重载) 在布局资源上设置活动内容。我应该对此感到担忧吗?跨度>
      • 我已经更新了我的答案,看一看,没有错误,因为一旦按照上面的代码更新,你就没有默认主题,它会消失@BahAkif
      • 我想我们快到了。我正在经历一个错误,它出现在 styles.xml 中,显示错误; 未找到与给定名称匹配的资源(位于“drawerArrowStyle”,值为“@style/DrawerArrowStyle”)。我正在使用一个名为 Android Support Library v7 AppCompat 的组件,并通过 Xamarin.incXamarin.Android.Support.v7.AppCompat 的块包/b>。我是否下载了正确的包和组件?
      • 是的,实际上我没有包含一种样式,抱歉,您可以检查上面的更新代码并且您已经安装了正确的 nuget 包@BahAkif
      • 它对&lt;item name="color"&gt;@color/colorRedText&lt;/item&gt; 给出了相同的错误,就像之前的 DrawerArrowStyle 一样。我尝试删除&lt;item name="color"&gt;@color/colorRedText&lt;/item&gt;,但仍然在SetContentView(Resource.layout.main) 处创建相同的unhandled exception 错误,这几乎可以追溯到相同的情况。我认为,错误来自我的 axml 中的某个地方。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2017-02-11
      • 2016-11-16
      • 1970-01-01
      • 2023-03-03
      • 2018-10-28
      • 1970-01-01
      相关资源
      最近更新 更多