从代码中打开弹出窗口会抛出异常

时间:2017-12-04 15:21:07

标签: c# xamarin.android popup

我正在使用VS Community 2017上的Xamarin Android中的应用程序。我想从Popup启动FAB - FloatingActionButton。我分别创建了弹出式xml布局,当我启动弹出窗口时,调试器抛出异常,但没有显示错误代码,所以我不知道导致问题的原因。

我最好的猜测是弹出窗口的xml布局文件有问题。我已经检查了很多次,但一切似乎都没问题。 android中的RelativePanel与UWP中的工作方式完全不同,也许我在那里做错了。

无论如何,这是代码...... 弹出布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
    android:text="Sort By:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:id="@+id/moviesSortByText"
    android:minHeight="28dp"
    android:gravity="left|center_vertical" />
<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/moviesSortSpinner"
    android:minWidth="120dp"
    android:minHeight="28dp"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@id/moviesSortByText" />
<TextView
    android:text="Genre:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:id="@+id/moviesGenresText"
    android:layout_below="@id/moviesSortByText"
    android:minHeight="28dp"
    android:gravity="left|center_vertical" />
<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/moviesSortByText"
    android:id="@+id/moviesGenresSpinner"
    android:minWidth="120dp"
    android:minHeight="28dp"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@id/moviesGenresText" />
<TextView
    android:text="Country:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:id="@+id/moviesCountryText"
    android:layout_below="@id/moviesGenresText"
    android:minHeight="28dp"
    android:gravity="left|center_vertical" />
<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/moviesGenresText"
    android:id="@+id/moviesCountrySpinner"
    android:minWidth="120dp"
    android:minHeight="28dp"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@id/moviesCountryText" />
</RelativeLayout>

活动背后的代码:

moviesOptionsBtn = FindViewById<FloatingActionButton>(Resource.Id.moviesOptionsFAB);
moviesOptionsBtn.Click += MoviesOptionsBtn_Click;

    private void MoviesOptionsBtn_Click(object sender, EventArgs e)
    {
        // create a flyout with 3 spinners for sorting, genres and countries
        var popupView = LayoutInflater.Inflate(Resource.Layout.moviesOptionsPopupLayout, null);
        var popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent, true)
        {
            Touchable = true,
            Focusable = true,
            OutsideTouchable = true
        };
        // find spinners and fill them with values
        sortSpinner = popupView.FindViewById<Spinner>(Resource.Id.moviesSortSpinner);
        genreSpinner = popupView.FindViewById<Spinner>(Resource.Id.moviesGenresSpinner);
        countrySpinner = popupView.FindViewById<Spinner>(Resource.Id.moviesCountrySpinner);
        sortSpinner.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Android.Resource.Layout.SimpleSpinnerDropDownItem, ViewHelper.SortList);
        genreSpinner.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Android.Resource.Layout.SimpleSpinnerDropDownItem, ViewHelper.GenresList);
        countrySpinner.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Android.Resource.Layout.SimpleSpinnerDropDownItem, ViewHelper.CountryList);
        // register selections
        sortSpinner.ItemSelected += MoviesOptionsSpinner_ItemSelected;
        genreSpinner.ItemSelected += MoviesOptionsSpinner_ItemSelected;
        countrySpinner.ItemSelected += MoviesOptionsSpinner_ItemSelected;

        popupWindow.ShowAsDropDown(moviesOptionsBtn);
    }

FAB显示正常,异常仅在最后一行之后抛出。所有其他代码似乎都运行良好。

有人可以查看我的代码,并告诉我是否有什么东西我做错了吗?

1 个答案:

答案 0 :(得分:0)

感谢您的评论,我找到了问题的根源。

在Android中,Spinner的适配器属性非常不同。正如我所做的那样,

中的第二和第三个参数之一
sortSpinner.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Android.Resource.Layout.SimpleSpinnerDropDownItem, ViewHelper.SortList);

TextView的资源,但我从Android框架传递了一个样式,这导致了异常。为了使它正确,我必须只为封闭的Spinner使用一种样式,然后为打开的列表设置第二个资源。

var spinnerAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, ViewHelper.SortList);
spinnerAdapter.SetDropdownResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
sortSpinner.Adapter = spinnerAdapter;

这解决了问题所以我认为我应该把它放在这里供将来参考。谢谢你的努力。