(MapFragment) FragmentManager.FindFragmentById (Resource.Id.map) всегда возвращает ноль - PullRequest
0 голосов
/ 02 ноября 2018

Я пытаюсь импортировать карту Google в свое приложение xamarin.android. Я установил ключ и написал код следующим образом: *

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Maps);

            SetUpMap();

            FindViewElements();
        }

      private void SetUpMap()
        {
            if (mMap == null)
            {
                    var _mapFragment = FragmentManager.FindFragmentByTag("maptag") as MapFragment;//null

                    var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);//null

                    // var mapFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map);//Here are also null

                   // var _mapFragment = SupportFragmentManager.FindFragmentById(Resource.Id.map) as SupportMapFragment; /null           
}
        }
     protected override async void FindViewElements()
        {
            _mapsModel = new MapsModel
                {
                    MapsFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map),
                    MapTypeSpinner = FindViewById<Spinner>(Resource.Id.spinner),
                    Toolbar = FindViewById<Toolbar>(Resource.Id.mapToolbar),
                };

                SetSupportActionBar(_mapsModel.Toolbar);

                SupportActionBar.Title = "Maps";

                SupportActionBar.SetHomeButtonEnabled(true);

                SupportActionBar.SetDisplayHomeAsUpEnabled(true);

                _mapsModel.MapsFragment.GetMapAsync(this);

                _mapsModel.MapTypeSpinner.ItemSelected += MapTypeSpinner_ItemSelected;
        }

У меня есть axml следующим образом

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/header">
        <include
            android:id="@+id/mapToolbar"
            layout="@layout/toolbar" />
    </RelativeLayout>
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/maptype_arrays" />
    <fragment
        android.id="@+id/map"
        android:tag="maptag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

Я перепробовал все возможные способы, которые я нашел, например, заменить Fragment на suppportFragment, но ничего не помогло. Может кто-нибудь, пожалуйста, предложите мне добиться этого.

Заранее спасибо

1 Ответ

0 голосов
/ 06 ноября 2018

Я пробую ваш axml и получаю результат как ваш, поэтому я предлагаю вам добавить фрагмент в FrameLayout, чтобы вы могли получить этот фрагмент по Id. Как это:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
    android:id="@+id/zoomInButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Zoom In" />
<Button
    android:id="@+id/zoomOutButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Zoom Out" />
<Button
    android:id="@+id/animateButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Animate to Location" />
<FrameLayout
    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"
        class="com.google.android.gms.maps.MapFragment" />
</FrameLayout>

 var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);

var mapFragment = (MapFragment) FragmentManager.FindFragmentById (Resource.Id.map);

Вы также можете посмотреть документ MSDN об импорте карты Google в свой проект: https://docs.microsoft.com/en-us/xamarin/android/platform/maps-and-location/maps/maps-api

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...