【问题标题】:The name 'SuportMapFragment' does not exist in the current context当前上下文中不存在名称“SuportMapFragment”
【发布时间】:2020-11-02 02:25:35
【问题描述】:

我已经在我的应用程序中实现了一个地图,虽然是在一个视图上。在创建了一些布局之后,我想实现一个底部导航视图,这意味着我必须使用片段。我已经将我的活动代码移动到名为 map_fragment 的片段中,起初我不确定在哪里使用 FindFragmentById 但根据findViewById in Fragment 这应该用于 OnCreateView() 生命周期方法。 我现在的主要问题是捕获地图片段,因为我收到错误 The name 'SuportMapFragment' does not exist in the current context when using

    var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.gmap);
        // Was able to use FragmentManager, but it returns null...
    // var mapFragment = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.gmap);
        mapFragment.GetMapAsync(this);

这是完整的代码

    public class map_fragment : Android.Support.V4.App.Fragment, IOnMapReadyCallback
    {
        public override void OnCreate(Bundle savedInstanceState)
            {
            base.OnCreate(savedInstanceState);
                // Create your fragment here       
        }
        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            // Capture the Map Fragment
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            base.OnCreateView(inflater, container, savedInstanceState);

            View view = inflater.Inflate(Resource.Layout.activity_main, container, false);
            var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.gmap);
            //var mapFragment = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.gmap);
            mapFragment.GetMapAsync(this);

            return view;
        }
        public void OnMapReady(GoogleMap _map)
        {
            // Some other code here
        }
}

根据要求,这是我的main_activity

   public class MainActivity : AppCompatActivity
    {
        BottomNavigationView bottomNavigation;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Initialize Xamarin Essentials for the Geolocation Service
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.main_frame);
            //var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            //if (toolbar != null)
            //{
            //    SetSupportActionBar(toolbar);
            //    SupportActionBar.SetDisplayHomeAsUpEnabled(false);
            //    SupportActionBar.SetHomeButtonEnabled(false);
            //}

            bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);

            bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;

            // Load the first fragment on creation
            LoadFragment(Resource.Id.profile_view);

        }

        private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
        {
            LoadFragment(e.Item.ItemId);
        }

        void LoadFragment(int id)
        {
            Android.Support.V4.App.Fragment fragment = null;
            switch (id)
            {
                case Resource.Id.profile_view:
                    fragment = profile_fragment.NewInstance();
                    break;
                case Resource.Id.map_view:
                    fragment = map_fragment.NewInstance();
                    break;
            }

            if (fragment == null)
                return;

            SupportFragmentManager.BeginTransaction()
                .Replace(Resource.Id.content_frame, fragment)
                .Commit();
        }

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

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

这些是我用作指南的教程

【问题讨论】:

  • 您所指的 SupportMapFragment 和 SuportMapFragment 有区别吗?您能否也分享一下布局文件 activity_main 的内容?你想学习什么教程?

标签: android google-maps xamarin android-fragments xamarin.android


【解决方案1】:

当您在Fragment 中使用SupportMapFragment 时,您可以这样称呼它:

SupportMapFragment mapFragment = Activity.SupportFragmentManager.FindFragmentById(Resource.Id.gmap).JavaCast<SupportMapFragment>();    
 if (mapFragment == null)
    {
     mapFragment = SupportMapFragment.NewInstance();
     mapFragment.GetMapAsync(this);
    }

在您的片段 xml 中引用 SupportMapFragment:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/gmap"
  class="com.google.android.gms.maps.SupportMapFragment" />

更新(像这样,我们可以处理OnMapReady):

SupportMapFragment mapFragment = ChildFragmentManager.FindFragmentById(Resource.Id.gmap).JavaCast<SupportMapFragment>();    
mapFragment.GetMapAsync(this);

【讨论】:

  • mapFragment 现在返回null,会不会和布局有关?
  • 你在布局中提到过吗?
  • View view = inflater.Inflate(Resource.Layout.activity_main, container, false);
  • 你检查上面的更新,把它添加到你的activity_main中。
  • @Jeremy 如果你想处理OnMapReady,我们应该做一些改变,见上面的更新,我们不使用mapFragment = SupportMapFragment.NewInstance();
猜你喜欢
  • 2013-10-02
  • 2014-04-22
  • 2017-06-17
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 2011-10-19
相关资源
最近更新 更多