MvxSpinner 在尝试绑定项目列表时因 Resources+NotFoundException 而崩溃
MvxSpinner crash with Resources+NotFoundException on an attempt to bind an items list
我曾经有一个 MvxSpinned 控件来 select 列表中的一个项目。最近从 MvvmCross 4.x 迁移到 5.x 后它停止工作,崩溃应用程序以尝试绑定 MvxSpinner
<MvxSpinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource MyList;SelectedItem ItemFromTheList;Visible IsItemSelectorEnabled" />
错误(崩溃)基本上是说在列表项视图中找不到某些资源 inflation
Java.Interop.JniEnvironmentResource ID #0x0
Raw
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<896ad1d315ca4ba7b117efb8dacaedcf>:0
Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualObjectMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args)<7cfbebb561c54efc9010b018c0846c7e>:0
Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)<7cfbebb561c54efc9010b018c0846c7e>:0
Android.Views.LayoutInflater.Inflate(int resource, ViewGroup root, bool attachToRoot)<e975227ac8644a30bb0866117325de0d>:0
MvvmCross.Binding.Droid.Views.MvxLayoutInflater.Inflate(int resource, ViewGroup root, bool attachToRoot)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate(int resourceId, ViewGroup viewGroup, bool attachToRoot)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate(int resourceId, ViewGroup viewGroup, bool attachToRoot)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxListItemView.MvxListItemView(Context context, IMvxLayoutInflaterHolder layoutInflaterHolder, object dataContext, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.CreateBindableView(object dataContext, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.GetBindableView(View convertView, object dataContext, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.GetView(int position, View convertView, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.GetView(int position, View convertView, ViewGroup parent)<0f7a6d7740764835ab4a872238640fdc>:0
Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_(IntPtr jnienv, IntPtr native__this, int position, IntPtr native_convertView, IntPtr native_parent)<e975227ac8644a30bb0866117325de0d>:0
at (wrapper dynamic-method) System.Object:a5f970b5-8275-430d-a4b9-72fe165e02cc (intptr,intptr,int,intptr,intptr)
--- End of managed Android.Content.Res.Resources+NotFoundException stack trace ---
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1266)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2652)
at android.content.res.Resources.getLayout(Resources.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
md568b3ec281d538477a14850a575f79ed6.MvxAdapter.n_getView(Native Method)
at md568b3ec281d538477a14850a575f79ed6.MvxAdapter.getView(MvxAdapter.java:92)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
at android.widget.Spinner.onMeasure(Spinner.java:507)
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:426)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1263)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.widget.ScrollView.onMeasure(ScrollView.java:337)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17565)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2045)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1196)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1409)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
遇到了和你一样的错误。
解决方案是在您的 Activity(或片段)中显式设置微调器的 ItemTemplateId 和 DropDownItemTemplateId,例如:
MvxAppCompatSpinner spinner = FindViewById<MvxAppCompatSpinner>(Resource.Id.spinner);
spinner.ItemTemplateId = Android.Resource.Layout.SimpleSpinnerItem;
spinner.DropDownItemTemplateId = Android.Resource.Layout.SimpleSpinnerDropDownItem;
以 Krum 的解决方案为基础,也可以直接在 XML 中直接设置 MvxItemTemplate
和 MvxDropDownItemTemplate
,以避免必须在视图代码中拦截微调器的创建:
local:MvxItemTemplate="@android:layout/simple_spinner_item"
local:MvxDropDownItemTemplate="@android:layout/simple_spinner_dropdown_item"
注意这里,我们需要指定模板,而不是id。
我曾经有一个 MvxSpinned 控件来 select 列表中的一个项目。最近从 MvvmCross 4.x 迁移到 5.x 后它停止工作,崩溃应用程序以尝试绑定 MvxSpinner
<MvxSpinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource MyList;SelectedItem ItemFromTheList;Visible IsItemSelectorEnabled" />
错误(崩溃)基本上是说在列表项视图中找不到某些资源 inflation
Java.Interop.JniEnvironmentResource ID #0x0
Raw
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()<896ad1d315ca4ba7b117efb8dacaedcf>:0
Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualObjectMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args)<7cfbebb561c54efc9010b018c0846c7e>:0
Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)<7cfbebb561c54efc9010b018c0846c7e>:0
Android.Views.LayoutInflater.Inflate(int resource, ViewGroup root, bool attachToRoot)<e975227ac8644a30bb0866117325de0d>:0
MvvmCross.Binding.Droid.Views.MvxLayoutInflater.Inflate(int resource, ViewGroup root, bool attachToRoot)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate(int resourceId, ViewGroup viewGroup, bool attachToRoot)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate(int resourceId, ViewGroup viewGroup, bool attachToRoot)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxListItemView.MvxListItemView(Context context, IMvxLayoutInflaterHolder layoutInflaterHolder, object dataContext, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.CreateBindableView(object dataContext, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.GetBindableView(View convertView, object dataContext, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.GetView(int position, View convertView, ViewGroup parent, int templateId)<0f7a6d7740764835ab4a872238640fdc>:0
MvvmCross.Binding.Droid.Views.MvxAdapter.GetView(int position, View convertView, ViewGroup parent)<0f7a6d7740764835ab4a872238640fdc>:0
Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_(IntPtr jnienv, IntPtr native__this, int position, IntPtr native_convertView, IntPtr native_parent)<e975227ac8644a30bb0866117325de0d>:0
at (wrapper dynamic-method) System.Object:a5f970b5-8275-430d-a4b9-72fe165e02cc (intptr,intptr,int,intptr,intptr)
--- End of managed Android.Content.Res.Resources+NotFoundException stack trace ---
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1266)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2652)
at android.content.res.Resources.getLayout(Resources.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
md568b3ec281d538477a14850a575f79ed6.MvxAdapter.n_getView(Native Method)
at md568b3ec281d538477a14850a575f79ed6.MvxAdapter.getView(MvxAdapter.java:92)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
at android.widget.Spinner.onMeasure(Spinner.java:507)
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:426)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1263)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.widget.ScrollView.onMeasure(ScrollView.java:337)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17565)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2045)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1196)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1409)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
遇到了和你一样的错误。 解决方案是在您的 Activity(或片段)中显式设置微调器的 ItemTemplateId 和 DropDownItemTemplateId,例如:
MvxAppCompatSpinner spinner = FindViewById<MvxAppCompatSpinner>(Resource.Id.spinner);
spinner.ItemTemplateId = Android.Resource.Layout.SimpleSpinnerItem;
spinner.DropDownItemTemplateId = Android.Resource.Layout.SimpleSpinnerDropDownItem;
以 Krum 的解决方案为基础,也可以直接在 XML 中直接设置 MvxItemTemplate
和 MvxDropDownItemTemplate
,以避免必须在视图代码中拦截微调器的创建:
local:MvxItemTemplate="@android:layout/simple_spinner_item"
local:MvxDropDownItemTemplate="@android:layout/simple_spinner_dropdown_item"
注意这里,我们需要指定模板,而不是id。