Xamarin,Android。 Google 地图和 AppCompat FragmentManager.FindFragmentById returns 空
Xamarin, Android. Google Maps and AppCompat FragmentManager.FindFragmentById returns null
我正在尝试将 Google 地图 ( 27.0.0.0 ) 与 Xamarin 上的 AppCompat 一起使用。一切正常,地图显示在片段中,但是当我尝试使用以下方法获取片段引用时:
SupportMapFragment m_mpMapFragment = ( SupportMapFragment ) FragmentManager.FindFragmentById( Resource.Id.Gmap );
我总是 m_mpMapFragment 为空。 我过去在另一个应用程序中遇到过这个错误,我修复它只是删除所有 Xamarin Support V7。但这次我使用的是 AppCompat,我无法从 V7 支持中删除引用。
我的项目有两个 Class:MainActivity 和 MapAroundMeFragment 在用户点击后在 Fragment 中显示地图一个标签栏。在 MainActivity 中我有 "Main.axml" 在我的 MapAroundMeFragment 中我有 "MapAroundMeFragmentLayout.xaml".
我找到了另一种方法来解决这个问题,使用这个 "trick":
SetContentView( Resource.Layout.MapAroundMeFragmentLayout );
SetContentView( Resource.Layout.Main );
在我的 MainActivity 中,因此,在我的 MapAroundMeFragment
中
SupportMapFragment m_mpMapFragment = ( SupportMapFragment ) FragmentManager.FindFragmentById( Resource.Id.Gmap );
还我一个有效的m_mpMapFragment!我不明白为什么。谁能帮帮我?
这是我的 MainActivity
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Gms.Maps;
using Android.Support.V7.App;
using ActionBar = Android.Support.V7.App.ActionBar;
using Fragment = Android.Support.V4.App.Fragment;
namespace OscarFinder_AndroidOS {
[Activity(Label = "OscarFinder_Android", Theme = "@style/Theme.Customab", Icon = "@drawable/ic_launcher", MainLauncher = true)]
public class MainActivity : ActionBarActivity, ActionBar.ITabListener {
private Fragment m_fCurrentFragment;
protected override void OnCreate( Bundle bundle ) {
base.OnCreate (bundle);
var setting = Android.Support.V7.App.ActionBar.NavigationModeTabs;
SupportActionBar.NavigationMode = setting;
// Set our view from the "main" layout resource
/* trick!! */SetContentView( Resource.Layout.MapAroundMeFragmentLayout );
SetContentView( Resource.Layout.Main );
CreateAndInitTabs();
}
private void CreateAndInitTabs() {
/* Tab riservata alla Home */
var homeTab = SupportActionBar.NewTab();
homeTab.SetText( "Home" );
homeTab.SetTabListener( this );
SupportActionBar.AddTab( homeTab );
/* Tab riservata alla Mappa */
var mapTab = SupportActionBar.NewTab();
mapTab.SetText( "Map" );
mapTab.SetTabListener( this );
SupportActionBar.AddTab( mapTab );
/* Tab riservata alla Mappa */
var optionTab = SupportActionBar.NewTab();
optionTab.SetText( "Option" );
optionTab.SetTabListener( this );
SupportActionBar.AddTab( optionTab );
}
public override bool OnOptionsItemSelected( IMenuItem iItem ) {
return base.OnOptionsItemSelected( iItem );
}
public void OnTabReselected( ActionBar.Tab iTab, Android.Support.V4.App.FragmentTransaction iFragment ) {
}
public void OnTabSelected( ActionBar.Tab iTab, Android.Support.V4.App.FragmentTransaction iFragment ) {
if ( iTab.Position == 0 ) {
m_fCurrentFragment = new HomeOscarFinderFragment();
}
else if ( iTab.Position == 1 ) {
m_fCurrentFragment = new MapAroundMeFragment();
}
iFragment.Replace( Android.Resource.Id.Content, m_fCurrentFragment );
}
public void OnTabUnselected( ActionBar.Tab iTab, Android.Support.V4.App.FragmentTransaction iFragment ) {
if ( m_fCurrentFragment != null ) {
iFragment.Detach( m_fCurrentFragment );
}
}
}
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
MapAroundMeFragment
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
namespace OscarFinder_AndroidOS {
public class MapAroundMeFragment : Fragment, IOnMapReadyCallback {
private GoogleMap m_gMap;
private SupportMapFragment m_mpMapFragment;
private float m_fCurrentZoom = 15.0f;
private CustomMapPosition m_pMapPosition;
public override void OnCreate( Bundle savedInstanceState ) {
base.OnCreate( savedInstanceState );
}
public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
var view = inflater.Inflate( Resource.Layout.MapAroundMeFragmentLayout, container, false );
m_mpMapFragment = ( SupportMapFragment ) FragmentManager.FindFragmentById( Resource.Id.Gmap );
m_mpMapFragment.GetMapAsync( this );
UpdateMap();
return view;
}
public void OnMapReady( GoogleMap googleMap ) {
m_gMap = googleMap;
}
private void UpdateMap() {
m_gMap = m_mpMapFragment.Map;
if ( m_gMap != null ) {
}
}
}
}
MapAroundMeFragmentLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/test_fragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:layout_below="@+id/LocationSpecsHeader"
android:id="@+id/Gmap"
android:tag="MyMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
app:cameraZoom="16"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
非常感谢!
我用这段代码解决了。
在MapAroundMeFragment
中
public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
var view = inflater.Inflate( Resource.Layout.MapAroundMeFragmentLayout, container, false );
/* Eggià,
* creo il fragment della mappa manualmente, perchè, per qualche motivo, non c'è
* modo di farlo funzionare prendendolo direttamente dall'inflate. */
MapAroundMeSupportMapFragment mapFragment = new MapAroundMeSupportMapFragment();
Android.Support.V4.App.FragmentTransaction transaction = FragmentManager.BeginTransaction();
transaction.Add( Resource.Id.map_container, mapFragment ).Commit();
mapFragment.GetMapAsync( this );
return view;
}
在MapAroundMeSupportFragment
public class MapAroundMeSupportMapFragment : SupportMapFragment {
public void onActivityCreated( Bundle savedInstanceState ) {
base.OnActivityCreated( savedInstanceState );
}
}
现在一切正常..我希望!
我正在尝试将 Google 地图 ( 27.0.0.0 ) 与 Xamarin 上的 AppCompat 一起使用。一切正常,地图显示在片段中,但是当我尝试使用以下方法获取片段引用时:
SupportMapFragment m_mpMapFragment = ( SupportMapFragment ) FragmentManager.FindFragmentById( Resource.Id.Gmap );
我总是 m_mpMapFragment 为空。 我过去在另一个应用程序中遇到过这个错误,我修复它只是删除所有 Xamarin Support V7。但这次我使用的是 AppCompat,我无法从 V7 支持中删除引用。
我的项目有两个 Class:MainActivity 和 MapAroundMeFragment 在用户点击后在 Fragment 中显示地图一个标签栏。在 MainActivity 中我有 "Main.axml" 在我的 MapAroundMeFragment 中我有 "MapAroundMeFragmentLayout.xaml".
我找到了另一种方法来解决这个问题,使用这个 "trick":
SetContentView( Resource.Layout.MapAroundMeFragmentLayout );
SetContentView( Resource.Layout.Main );
在我的 MainActivity 中,因此,在我的 MapAroundMeFragment
中SupportMapFragment m_mpMapFragment = ( SupportMapFragment ) FragmentManager.FindFragmentById( Resource.Id.Gmap );
还我一个有效的m_mpMapFragment!我不明白为什么。谁能帮帮我?
这是我的 MainActivity
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Gms.Maps;
using Android.Support.V7.App;
using ActionBar = Android.Support.V7.App.ActionBar;
using Fragment = Android.Support.V4.App.Fragment;
namespace OscarFinder_AndroidOS {
[Activity(Label = "OscarFinder_Android", Theme = "@style/Theme.Customab", Icon = "@drawable/ic_launcher", MainLauncher = true)]
public class MainActivity : ActionBarActivity, ActionBar.ITabListener {
private Fragment m_fCurrentFragment;
protected override void OnCreate( Bundle bundle ) {
base.OnCreate (bundle);
var setting = Android.Support.V7.App.ActionBar.NavigationModeTabs;
SupportActionBar.NavigationMode = setting;
// Set our view from the "main" layout resource
/* trick!! */SetContentView( Resource.Layout.MapAroundMeFragmentLayout );
SetContentView( Resource.Layout.Main );
CreateAndInitTabs();
}
private void CreateAndInitTabs() {
/* Tab riservata alla Home */
var homeTab = SupportActionBar.NewTab();
homeTab.SetText( "Home" );
homeTab.SetTabListener( this );
SupportActionBar.AddTab( homeTab );
/* Tab riservata alla Mappa */
var mapTab = SupportActionBar.NewTab();
mapTab.SetText( "Map" );
mapTab.SetTabListener( this );
SupportActionBar.AddTab( mapTab );
/* Tab riservata alla Mappa */
var optionTab = SupportActionBar.NewTab();
optionTab.SetText( "Option" );
optionTab.SetTabListener( this );
SupportActionBar.AddTab( optionTab );
}
public override bool OnOptionsItemSelected( IMenuItem iItem ) {
return base.OnOptionsItemSelected( iItem );
}
public void OnTabReselected( ActionBar.Tab iTab, Android.Support.V4.App.FragmentTransaction iFragment ) {
}
public void OnTabSelected( ActionBar.Tab iTab, Android.Support.V4.App.FragmentTransaction iFragment ) {
if ( iTab.Position == 0 ) {
m_fCurrentFragment = new HomeOscarFinderFragment();
}
else if ( iTab.Position == 1 ) {
m_fCurrentFragment = new MapAroundMeFragment();
}
iFragment.Replace( Android.Resource.Id.Content, m_fCurrentFragment );
}
public void OnTabUnselected( ActionBar.Tab iTab, Android.Support.V4.App.FragmentTransaction iFragment ) {
if ( m_fCurrentFragment != null ) {
iFragment.Detach( m_fCurrentFragment );
}
}
}
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
MapAroundMeFragment
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
namespace OscarFinder_AndroidOS {
public class MapAroundMeFragment : Fragment, IOnMapReadyCallback {
private GoogleMap m_gMap;
private SupportMapFragment m_mpMapFragment;
private float m_fCurrentZoom = 15.0f;
private CustomMapPosition m_pMapPosition;
public override void OnCreate( Bundle savedInstanceState ) {
base.OnCreate( savedInstanceState );
}
public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
var view = inflater.Inflate( Resource.Layout.MapAroundMeFragmentLayout, container, false );
m_mpMapFragment = ( SupportMapFragment ) FragmentManager.FindFragmentById( Resource.Id.Gmap );
m_mpMapFragment.GetMapAsync( this );
UpdateMap();
return view;
}
public void OnMapReady( GoogleMap googleMap ) {
m_gMap = googleMap;
}
private void UpdateMap() {
m_gMap = m_mpMapFragment.Map;
if ( m_gMap != null ) {
}
}
}
}
MapAroundMeFragmentLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/test_fragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:layout_below="@+id/LocationSpecsHeader"
android:id="@+id/Gmap"
android:tag="MyMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
app:cameraZoom="16"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
非常感谢!
我用这段代码解决了。
在MapAroundMeFragment
中public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
var view = inflater.Inflate( Resource.Layout.MapAroundMeFragmentLayout, container, false );
/* Eggià,
* creo il fragment della mappa manualmente, perchè, per qualche motivo, non c'è
* modo di farlo funzionare prendendolo direttamente dall'inflate. */
MapAroundMeSupportMapFragment mapFragment = new MapAroundMeSupportMapFragment();
Android.Support.V4.App.FragmentTransaction transaction = FragmentManager.BeginTransaction();
transaction.Add( Resource.Id.map_container, mapFragment ).Commit();
mapFragment.GetMapAsync( this );
return view;
}
在MapAroundMeSupportFragment
public class MapAroundMeSupportMapFragment : SupportMapFragment {
public void onActivityCreated( Bundle savedInstanceState ) {
base.OnActivityCreated( savedInstanceState );
}
}
现在一切正常..我希望!