所以我有这个基本的Xamarin Android应用程序,其中Home Activity包含几个片段。每个片段像往常一样容纳一个层次的视图。 应用程序具有自动注销功能(一旦用户登录),如果设备闲置一段时间,则该设备将被注销。在自动注销后,当用户打开应用程序并登录时,将显示最后一个片段(当用户离开应用程序时)。
我正面临着一个随机的崩溃,在这个阶段,它看起来已经处理或垃圾收集,但应用程序仍然引用了旧的碎片。堆栈跟踪看起来像 -
FATAL EXCEPTION: main
01-05 13:58:51.941 3292 3292 E AndroidRuntime: Process: PinForce.Mobile, PID: 3292
01-05 13:58:51.941 3292 3292 E AndroidRuntime: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: ... 1 more
01-05 13:58:51.941 3292 3292 E AndroidRuntime: Caused by: android.runtime.JavaProxyThrowable: System.NotSupportedException: Unable to activate instance of type PinForce.Framework.Survey.Display.SearchFormView from native handle 0xffc7616c (key_handle 0x391202b). ---> System.MissingMethodException: No constructor found for PinForce.Framework.Survey.Display.SearchFormView::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
01-05 13:58:51.941 3292 3292 E AndroidRuntime: --- End of inner exception stack trace ---
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00058] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00138] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: --- End of inner exception stack trace ---
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x001b0] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000e5] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x0001a] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Java.Lang.Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00006] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at Android.Views.View.n_DispatchTouchEvent_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x00000] in <64fedfa134eb440aa45b8be4ef49a962>:0
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at (wrapper dynamic-method) System.Object:3cb395be-06f7-4df6-8dc1-7552ee0d6361 (intptr,intptr,intptr)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at md5c50fdf4dafbb5388e2f5304a2b7613f8.SearchFormView.n_dispatchTouchEvent(Native Method)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at md5c50fdf4dafbb5388e2f5304a2b7613f8.SearchFormView.dispatchTouchEvent(SearchFormView.java:54)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2799)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2799)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2514)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.cancelTouchTarget(ViewGroup.java:2717)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4943)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4923)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.view.ViewGroup.removeView(ViewGroup.java:4854)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1270)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:758)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1552)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:487)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
01-05 13:58:51.941 3292 3292 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7229)
我读了一下,看起来像是一个过早处置的情况。我应该提一下,我的大多数视图都实现了dispose模式并释放了所有内存。
我的问题是我该如何处理?我应该实现特殊的(IntPtr, JniHandleOwnership) constructor
吗?这个特殊的构造函数我想可以防止崩溃,但它会在我的场景中有用吗?
注意:我正在试图找出为什么我的观点过早被处置的步骤。我想解决这个问题应该解决我的问题,但在此之前需要一个占位符解决方案。已经花了几天时间。
修改 -
public override void OnDestroyView()
{
if (_ssv != null)
{
_ssv.Dispose();
_ssv = null;
}
base.OnDestroyView();
}
上面的方法是我的一个片段。在视图销毁时,我试图手动处理所有视图。请注意,在原生Android中,我们通常不会做这样的事情,因为框架负责在销毁容器Activity或片段时重新关联任何视图引用。但我们正在从Xamarin的早期版本开始研究这个应用程序,这些版本不稳定且存在许多与内存相关的问题。因此,为了避免它们,我们所有的视图都实现了dispose模式,并在活动或片段被销毁时调用。现在的问题是,我应该稍后将此推迟到片段的onDestroy()