我在Xamarin Android应用程序中使用Mvvmcross。我试图使用PopupWindow与RecyclerView加载项目。但是,我得到"对象引用未设置为对象的实例"显示弹出窗口时出现异常。以下是堆栈跟踪
[MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object.
[MonoDroid] at MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerAdapter.OnCreateViewHolder (Android.Views.ViewGroup parent, System.Int32 viewType) [0x00000] in C:\projects\mvvmcross\MvvmCross-AndroidSupport\MvvmCross.Droid.Support.V7.RecyclerView\MvxRecyclerAdapter.cs:155
[MonoDroid] at Android.Support.V7.Widget.RecyclerView+Adapter.n_OnCreateViewHolder_Landroid_view_ViewGroup_I (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_parent, System.Int32 viewType) [0x0000f] in <1c3b474ba0bc45cba6ec33f4b4b4b99a>:0
[MonoDroid] at (wrapper dynamic-method) System.Object:19c45b14-7829-49b5-9757-906d8e518696 (intptr,intptr,intptr,int)
下面是我用来在片段中显示PopupWindow的代码片段。
myTextControl.Click += delegate
{
View contentView = LayoutInflater.From(Activity).Inflate(Resource.Layout.custom_filter_dropdown, rootElement, false);
MvxRecyclerView myRecyclerView = (MvxRecyclerView)contentView.FindViewById<RecyclerView>(Resource.Id.rv_function_wash_time);
myRecyclerView.Id = 1;
myRecyclerView.ItemsSource = new string[] { "Item1", "Item2"};
PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
myRecyclerView.ItemTemplateId = Resource.Id.filterdropdowntemplate;
popupWindow.SetBackgroundDrawable(new BitmapDrawable());
popupWindow.OutsideTouchable = true;
popupWindow.ShowAsDropDown(myTextControl, 0, 0);
};
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<MvxRecyclerView
android:id="@+id/rv_function_wash_time"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</MvxRecyclerView>
</LinearLayout>
我在整个应用程序的各个地方使用MvxRecylerView。我只面临问题如果我尝试在PopupWindow中使用它。请帮忙!
答案 0 :(得分:0)
我认为问题是你不是通过BindingInflate
来夸大你的布局。由于MvxRecyclerAdapter
中的line 155说明需要您未提供的绑定上下文。
var contentView = this.BindingInflate(Resource.Layout.custom_filter_dropdown, rootElement);