android视图中的null引用异常可见性绑定

时间:2013-05-15 19:36:42

标签: android xamarin.android mvvmcross

我有一个带有几个项目的MvxListView。 每个项目由2个组件组成,一个是可见的,另一个不是(Gone)。 已消失的组件可见性属性绑定到视图模型上的布尔属性,带有 布尔可见性值转换器。

在列表项上单击,我将布尔属性更改为true,这使得已删除的组件 变得可见,为该特定项目实现某种扩展效果。

99%的情况下,这种方法非常有效。

有一段时间,由于某种原因,我得到以下堆栈跟踪的空引用异常:

691.32 Problem seen during binding execution for from IsInEditMode to Visibility - problem TargetInvocationException: Exception has been thrown by the target of an invocation.
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (System.Object value) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (Boolean isAvailable, System.Object value) [0x00000] in <filename unknown>:0 
  InnerException was NullReferenceException: Object reference not set to an instance of an object

我不确定这是否是MvvmCross问题,或者是android视图问题,但是注意到已经消失的组件有一个按钮可能很重要。因此,虽然列表中的所有项目都已折叠,但没有按钮,当我展开1项时,有一个按钮,整个列表项变得无法点击,但按钮是可点击的(这是一个无关的错误我打算修复有一天,但目前对我来说并不重要。)

我正在使用MvvmCross v3。我承认我没有使用最新的版本(1个月大), 虽然值得一试,看看这是否会发生新的比特,但这个问题很少见 难以重现。

我有更详细的堆栈跟踪信息:

at (wrapper delegate-invoke) <Module>.invoke_void__this___intptr_intptr_intptr_JValue[]   (intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x00062, 0xffffffff>
at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00000] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:364
at Android.Views.View.set_Visibility (Android.Views.ViewStates) [0x0003e] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Views.View.cs:4498
at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___int (object,intptr,intptr,intptr) <IL 0x00054, 0xffffffff>
at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x000c6, 0x00553>
at System.Reflection.MonoProperty.SetValue (object,object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x0006c, 0x00353>
at System.Reflection.PropertyInfo.SetValue (object,object,object[]) <IL 0x00007, 0x000a3>
at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue (object) <IL 0x0007b, 0x0033f>
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource (bool,object) <IL 0x00054, 0x001f3>
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.<CreateSourceBinding>b__0 (object,Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourcePropertyBindingEventArgs) <IL 0x0000d, 0x00083>
at Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourceBinding.FireChanged (Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourcePropertyBindingEventArgs) <IL 0x0000d, 0x000bf>
at Cirrious.MvvmCross.Binding.Bindings.Source.Leaf.MvxLeafPropertyInfoSourceBinding.OnBoundPropertyChanged () <IL 0x00007, 0x00073>
at Cirrious.MvvmCross.Binding.Bindings.Source.MvxPropertyInfoSourceBinding.SourcePropertyChanged (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00014, 0x000ab>
at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object_object (object,intptr,intptr,intptr) <IL 0x0005a, 0xffffffff>
at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x000c6, 0x00553>
at System.Reflection.MethodBase.Invoke (object,object[]) <IL 0x00006, 0x0008f>
at Cirrious.MvvmCross.Binding.WeakSubscription.MvxWeakEventSubscription`2.OnSourceEvent (object,TEventArgs) <IL 0x0002b, 0x00137>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00073>
at (wrapper delegate-invoke) <Module>.invoke_void__this___object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00059, 0xffffffff>
at Cirrious.MvvmCross.ViewModels.MvxNotifyPropertyChanged/<>c__DisplayClass1.<RaisePropertyChanged>b__0 () <IL 0x00021, 0x000f7>
at Cirrious.MvvmCross.Droid.Views.MvxAndroidMainThreadDispatcher/<>c__DisplayClass1.<RequestMainThreadAction>b__0 (object) <IL 0x00006, 0x0005b>
at Android.App.SyncContext.Post (System.Threading.SendOrPostCallback,object) [0x00025] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:19
at Cirrious.MvvmCross.Droid.Views.MvxAndroidMainThreadDispatcher.RequestMainThreadAction (System.Action) <IL 0x0001f, 0x00173>
at Cirrious.CrossCore.Core.MvxMainThreadDispatchingObject.InvokeOnMainThread (System.Action) <IL 0x0000f, 0x00097>
at Cirrious.MvvmCross.ViewModels.MvxNotifyPropertyChanged.RaisePropertyChanged (string) <IL 0x0002a, 0x001ff>
at Cirrious.MvvmCross.ViewModels.MvxNotifyPropertyChanged.RaisePropertyChanged<bool> (System.Linq.Expressions.Expression`1<System.Func`1<bool>>) <IL 0x0000a, 0x00087>
at Game.GamePlayerViewModel.set_IsInEditMode (bool) [0x00008] in c:\Users\Roman\Documents\Projects\Games\Game\ViewModels\Players\GamePlayerViewModel.cs:74
at Game.GamePlayersViewModel.HandleSelectPlayer (Game.GamePlayerViewModel) [0x0000f] in c:\Users\Roman\Documents\Projects\Game\Game\ViewModels\Players\GamePlayersViewModel.cs:117
at Cirrious.MvvmCross.ViewModels.MvxCommand`1.Execute (object) <IL 0x00015, 0x000bb>
at Cirrious.MvvmCross.Binding.Droid.Views.MvxListView.ExecuteCommandOnItem (System.Windows.Input.ICommand,int) <IL 0x00021, 0x001b7>
at Cirrious.MvvmCross.Binding.Droid.Views.MvxListView.<SetupItemClickListeners>b__0 (object,Android.Widget.AdapterView/ItemClickEventArgs) <IL 0x0000d, 0x00087>
at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000b] in /Users/builder/data/lanes/monodroid-lion-    bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Widget.AdapterView.cs:233
at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00018] in /Users/builder/data/lanes/monodroid-lion-   bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Widget.AdapterView.cs:170
at (wrapper dynamic-method) object.5e36d7c3-b7bd-4ceb-af22-a12ff6cf17a9 (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x0009b>
at (wrapper native-to-managed) object.5e36d7c3-b7bd-4ceb-af22-a12ff6cf17a9 (intptr,intptr,intptr,intptr,int,long) <IL 0x00024, 0xffffffff>

此外,可见性绑定到属性的组件是LinearLayout。

有什么想法吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

堆栈跟踪看起来就像是从viewmodel到UI的调用。

它建议在UI组件set的{​​{1}}操作期间的某个地方,内部的代码正在点击Visibility - 但我看不到更多细节来自堆栈跟踪。

NullReferenceException位于View中set方法的某个位置。它是在NullReferenceException完成它的工作之后。

我无法从可用的信息中找到解决方案,而且在最近的v3修复程序中我没有任何理解可以解决这个问题。我猜这可能与超出范围的行/视图有关。为了解决这个问题,我想也许你需要弄清楚如何使其更具可重复性(超过1%)。

进一步调查此问题的一些方法:

  1. 您是否可以捕获更多堆栈跟踪信息 - 也可能包括logcat跟踪?
  2. 你能更频繁地发生这种情况吗?
  3. 您是否可以在较小的特定于Repro的测试项目中获得相同的效果?
  4. 你可以试试'运气'的事情吗 - 例如如果您尝试更改项目的高度而不是可见性会怎样?
  5. 你能在这里提供更多信息吗?例如什么是ValueConverter谁具有约束力?它只是一个标准的小部件吗?