【问题标题】:"Error inflating class fragment" with google map on xamarin在 xamarin 上使用谷歌地图“膨胀类片段时出错”
【发布时间】:2019-09-04 23:57:43
【问题描述】:

我有一个适用于 Android 应用程序的 xamarin。我想要一个带有标签的 ActionBar 和一个标签上的谷歌地图。

当我点击地图选项卡时,它只有一次。

我解释说,应用程序启动,我在事件选项卡上,我点击地图选项卡,地图工作正常,但是当我返回事件选项卡并再次地图选项卡时,它崩溃了。

主要:

ActionBar.SetDisplayShowHomeEnabled (true);
ActionBar.SetDisplayShowHomeEnabled (true);
ActionBar.SetDisplayShowTitleEnabled (true);
ActionBar.SetTitle (Resource.String.app_name);
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

SetContentView (Resource.Layout.Main);

var fragMap = new MapPageFragment();
ActionBar.Tab tab2 = ActionBar.NewTab();
tab2 = ActionBar.NewTab();
tab2.SetText("Map");
tab2.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
     {
         e.FragmentTransaction.Replace(Resource.Id.fragment_container, fragMap);
};
ActionBar.AddTab(tab2);

MapPageFragment:

namespace OnmappTest
{
    public class MapPageFragment : Android.App.Fragment
    {
         public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
         {
        // Use this to return your custom view for this Fragment
        view = inflater.Inflate(Resource.Layout.MapPage, container, false);

错误

二进制 xml 文件第 1 行:错误膨胀类片段”在“view = inflater.Inflate(Resource.Layout.MapPage, container, false);

地图页面

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

清单:

<?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="OnmappTest.OnmappTest">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="21" />
<!-- 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" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:label="OnmappTest" android:icon="@drawable/icon">
    <!-- Put your Google Maps V2 API Key here. -->
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="value" />
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>

我试过SupportMapFragment而不是MapFragment,FragmentActivity而不是Fragment但什么都没有

【问题讨论】:

    标签: google-maps android-fragments xamarin


    【解决方案1】:

    我遇到了同样的问题。请将此添加到 OnPause 方法中:

     protected override void OnPause()
        {
            base.OnPause();
            if (_mapFragment != null)
            {
                FragmentManager fragmentManager = FragmentManager;
                fragmentManager.BeginTransaction().Remove(_mapFragment).Commit();
            }
        }
    

    这解决了我的问题,希望能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2014-04-04
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      相关资源
      最近更新 更多