二进制 XML 文件第 1 行:Xamarin Android 中 Google Maps 的类片段错误膨胀错误

Posted

技术标签:

【中文标题】二进制 XML 文件第 1 行:Xamarin Android 中 Google Maps 的类片段错误膨胀错误【英文标题】:Binary XML file line #1: Error inflating class fragment Error for Google Maps in Xamarin Android 【发布时间】:2020-09-09 13:27:16 【问题描述】:

我在 SetContentView(Resource.Layout.activity_main); 的问题标题中遇到错误;当我运行应用程序时。

我有两个 XML 文件。 include_main.xml 包含地图片段所在的框架布局。使用属性 "class" 会给我这个错误 - “缺少 android XML 命名空间;您的属性将被解释为自定义属性”。如果我将类更改为 android:name,它会消除该错误,但我仍然会收到二进制 xml 文件错误。如果我完全取出地图片段代码,应用程序运行完全正常。

include_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView"
    android:orientation="vertical"
    android:layout_
    android:layout_>

    <FrameLayout
        android:layout_
        android:layout_
        android:background="@color/ashGrey">

        <fragment 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_
          android:layout_
          class="com.google.android.gms.maps.SupportMapFragment" />


        <android.support.v7.widget.Toolbar
            android:layout_
            android:layout_
            android:minHeight="?android:attr/actionBarSize"
            android:id="@+id/mainToolbar"/>

        <LinearLayout
           android:orientation="vertical"
            android:layout_
            android:layout_
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="70dp">
                <FrameLayout
                android:id="@+id/frameLocations"
                android:layout_
                android:layout_
                android:background="@drawable/roundedEdges">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_
                        android:layout_>
                            <RelativeLayout 
                                android:layout_
                                android:layout_
                                android:clickable="true"
                                android:layout_weight="0.5"
                                android:id="@+id/layoutPickup">

                                    <ImageView
                                        android:layout_
                                        android:layout_
                                        android:src="@drawable/greenball"
                                        android:layout_centerVertical="true"
                                        android:layout_marginLeft="10dp"/>

                                    <TextView 
                                        android:id="@+id/pickupLocationText"
                                        android:layout_
                                        android:layout_
                                        android:text="Pickup Location"
                                        android:layout_centerVertical="true"
                                        android:layout_marginLeft="30dp"
                                        android:maxLines="2"
                                        android:gravity="center_vertical"
                                        android:layout_marginRight="36dp"/>

                                    <RadioButton
                                        android:id="@+id/DestinationRadio"
                                        android:layout_
                                        android:layout_
                                        android:layout_gravity="end"
                                        android:layout_alignParentRight="true"
                                        android:layout_centerVertical="true"
                                        android:layout_marginRight="10dp"
                                    />

                                    <View
                                        android:layout_
                                        android:layout_
                                        android:layout_gravity="end"
                                        android:layout_alignParentBottom="true"
                                        android:background="@color/ashGrey"
                                    /> 
                            </RelativeLayout>
                            <RelativeLayout 
                                android:layout_
                                android:layout_
                                android:clickable="true"
                                android:layout_weight="0.5"
                                android:id="@+id/layoutDestination">
                                    <ImageView
                                        android:layout_
                                        android:layout_
                                        android:src="@drawable/redball"
                                        android:layout_centerVertical="true"
                                        android:layout_marginLeft="10dp" 
                                    />

                                    <TextView 
                                        android:id="@+id/destinationText"
                                        android:layout_
                                        android:layout_
                                        android:text="Destination"
                                        android:layout_centerVertical="true"
                                        android:layout_marginLeft="30dp"
                                        android:maxLines="2"
                                        android:gravity="center_vertical"
                                        android:layout_marginRight="36dp"/>


                                    <RadioButton
                                        android:id="@+id/DestinationRadio"
                                        android:layout_
                                        android:layout_
                                        android:layout_gravity="end"
                                        android:layout_alignParentRight="true"
                                        android:layout_centerVertical="true"
                                        android:layout_marginRight="10dp"
                                    />

                            </RelativeLayout>
                    </LinearLayout>
                </FrameLayout>
        </LinearLayout>

        <RelativeLayout
            android:layout_
            android:layout_
            android:clickable="true"
            android:id="@+id/myLocation"
            android:layout_gravity="center | right"
            android:background="@drawable/roundedEdges"
            android:layout_marginRight="16dp">

                <ImageView
                    android:layout_
                    android:layout_
                    android:tint="@color/colorAccent"
                android:padding="5dp"
                android:src="@mipmap/ic_my_location_black_48dp"/>
        </RelativeLayout>
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:id="@+id/drawerLayout"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_
         android:layout_
         android:orientation="vertical"
        >
        <include
            layout="@layout/include_main">
        </include>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navView"
        android:layout_
        android:layout_
        android:background="@color/white"
        android:layout_gravity="start"
        app:headerLayout="@layout/navHeader"
        />
</android.support.v4.widget.DrawerLayout>

Main_Activity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Firebase;
using Firebase.Database;
using Android.Views;
using Android.Gms.Maps;

namespace Semoto

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = false)]
    public class MainActivity : AppCompatActivity,  IOnMapReadyCallback
    
        FirebaseDatabase test;
        Android.Support.V4.Widget.DrawerLayout drawerLayout;
        Android.Support.V7.Widget.Toolbar mainToolbar;
        GoogleMap mainMap;
        protected override void OnCreate(Bundle savedInstanceState)
        
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            ConnectControl();

            //SupportMapFragment mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.map);
            //mapFragment.GetMapAsync(this);
            SupportMapFragment _mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.map);;
            if (_mapFragment == null)
            
                GoogleMapOptions mapOptions = new GoogleMapOptions()
                    .InvokeRotateGesturesEnabled(true)
                    .InvokeScrollGesturesEnabled(true)
                    .InvokeCompassEnabled(true)
                    .InvokeAmbientEnabled(true)
                    .InvokeMapType(GoogleMap.MapTypeNormal)
                    .InvokeZoomControlsEnabled(true)
                    .InvokeCompassEnabled(true);

                _mapFragment = SupportMapFragment.NewInstance(mapOptions);

                Android.Support.V4.App.FragmentTransaction fragTx = SupportFragmentManager.BeginTransaction();
                fragTx.Add(Resource.Id.map, _mapFragment, "map");
                fragTx.Commit();

                _mapFragment.GetMapAsync(this);
            
        

        /*public override bool OnCreateOptionsMenu(IMenu menu)
        
            this.MenuInflater.Inflate(Resource.Menu.sideMenuBar, menu);
            return base.OnCreateOptionsMenu(menu);
        
        public override bool OnCreateOptionsMenu(IMenu menu)
    
        this.MenuInflater.Inflate(Resource.Menu.Testing_New_List, menu);
        return base.OnCreateOptionsMenu(menu);
    
         */

        void ConnectControl()
        
            drawerLayout = (Android.Support.V4.Widget.DrawerLayout)FindViewById(Resource.Id.drawerLayout);
            mainToolbar = (Android.Support.V7.Widget.Toolbar)FindViewById(Resource.Id.mainToolbar);
            SetSupportActionBar(mainToolbar);
            SupportActionBar.Title = "Where are you heading?";
            //Android.Support.V7.App.ActionBar actionBar = SupportActionBar;
            //ActionBar.SetDisplayHomeAsUpEnabled(true);
            //ActionBar.SetHomeAsUpIndicator(Resource.Drawable.abc_ic_star_half_black_36dp);
        

        public override bool OnOptionsItemSelected(IMenuItem item)
        
            switch (item.ItemId)
            
                case Android.Resource.Id.Home:
                    drawerLayout.OpenDrawer((int)GravityFlags.Left);
                    return true;

                default:
                    return base.OnOptionsItemSelected(item);
            
        

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        

        public void OnMapReady(GoogleMap googleMap)
        
            mainMap = googleMap;
        
    

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.masslabs.semoto" android:installLocation="auto">
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="com.masslabs.semoto.permission.MAPS_RECEIVE" />
  <permission android:name="com.masslabs.semoto.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
  <uses-feature android:glEsVersion="0x00020000" android:required="true" />
  <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" android:name="android.app.Application" android:debuggable="true">
    <activity android:label="@string/app_name" android:theme="@style/AppTheme" android:name="crc64a220455bad8a5d5e.MainActivity" />
    <activity android:label="logInActivity" android:noHistory="false" android:screenOrientation="portrait" android:theme="@style/SignInPage" android:name="crc6401374d2456a4365c.logInActivity" />
    <activity android:label="newMemberActivity" android:screenOrientation="portrait" android:theme="@style/newMember" android:name="crc6401374d2456a4365c.newMemberActivity" />
    <activity android:label="registerActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme" android:name="crc6401374d2456a4365c.registerActivity" />
    <activity android:label="@string/app_name" android:noHistory="true" android:screenOrientation="portrait" android:theme="@style/MyTheme.Splash" android:name="crc6401374d2456a4365c.splashActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <service android:name="crc64a98abb514ffad9f1.KeepAliveService" />
    <receiver android:enabled="true" android:exported="false" android:label="Essentials Battery Broadcast Receiver" android:name="crc64a0e0a82d0db9a07d.BatteryBroadcastReceiver" />
    <receiver android:enabled="true" android:exported="false" android:label="Essentials Energy Saver Broadcast Receiver" android:name="crc64a0e0a82d0db9a07d.EnergySaverBroadcastReceiver" />
    <receiver android:enabled="true" android:exported="false" android:label="Essentials Connectivity Broadcast Receiver" android:name="crc64a0e0a82d0db9a07d.ConnectivityBroadcastReceiver" />
    <provider android:authorities="com.masslabs.semoto.fileProvider" android:exported="false" android:grantUriPermissions="true" android:name="xamarin.essentials.fileProvider">
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/xamarin_essentials_fileprovider_file_paths" />
    </provider>
    <provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="1999999999" android:authorities="com.masslabs.semoto.mono.MonoRuntimeProvider.__mono_init__" />
    <!--suppress ExportedReceiver-->
    <receiver android:name="mono.android.Seppuku">
      <intent-filter>
        <action android:name="mono.android.intent.action.SEPPUKU" />
        <category android:name="mono.android.intent.category.SEPPUKU.com.masslabs.semoto" />
      </intent-filter>
    </receiver>
    <provider android:authorities="com.masslabs.semoto.firebaseinitprovider" android:name="com.google.firebase.provider.FirebaseInitProvider" android:exported="false" android:initOrder="100" />
    <activity android:name="com.google.android.gms.common.api.GoogleApiActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:exported="false" />
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  </application>
  <!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
  <application android:label="@string/app_name">

    <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" />
    <!-- Necessary for apps that target Android 9.0 or higher -->
  </application>
</manifest>

【问题讨论】:

可以吗? @LeoZhu-MSFT 我还不确定。最近我真的很忙于另一个项目。所以当我可以再次处理这个项目时,我会回复你。 好的,如果有任何更新,请告诉我。 【参考方案1】:

尝试改变

<fragment 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_
      android:layout_
      class="com.google.android.gms.maps.SupportMapFragment" />

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

在活动中使用:

 SupportMapFragment _mapFragment = SupportFragmentManager.FindFragmentById(Resource.Id.map).JavaCast<SupportMapFragment>(); // use JavaCast<> as a workaround.
 if (_mapFragment == null)
        
            GoogleMapOptions mapOptions = new GoogleMapOptions()
                .InvokeRotateGesturesEnabled(true)
                .InvokeScrollGesturesEnabled(true)
                .InvokeCompassEnabled(true)
                .InvokeAmbientEnabled(true)
                .InvokeMapType(GoogleMap.MapTypeNormal)
                .InvokeZoomControlsEnabled(true)
                .InvokeCompassEnabled(true);

            _mapFragment = SupportMapFragment.NewInstance(mapOptions);
            _mapFragment.GetMapAsync(this);
        

别忘了在AndroidManifest.xml下面添加

<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_API_KEY" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

【讨论】:

我仍然收到类属性的“缺少 Android XML 命名空间;您的属性将被解释为自定义属性”错误。我也收到 Activity.FragmentManager 的警告。它说它已经过时了:已弃用 @Ashley 请检查上面的更新。使用 JaveCast 作为解决方法。我发现,如果你使用 class="com.google.android.gms.maps.SupportMapFragment",它也可以工作,你可以忽略它的错误

以上是关于二进制 XML 文件第 1 行:Xamarin Android 中 Google Maps 的类片段错误膨胀错误的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 28 行:二进制 XML 文件第 28 行:膨胀类 <unknown> 时出错

android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错

android.view.InflateException 膨胀类片段时出错,二进制 XML 文件第 1 行?

Google Map API 问题:android.view.InflateException:二进制 XML 文件第 2 行:二进制 XML 文件第 2 行:膨胀类片段时出错

当我切换到包含片段的活动时应用程序崩溃(二进制 XML 文件第 10 行:二进制 XML 文件第 10 行:膨胀类片段时出错)

android.view.inflateexception 二进制 xml 文件第 1 行错误膨胀类 android.widget.relativeLayout