无法访问xamarin表单项目中的已处置对象异常

时间:2017-11-10 12:41:56

标签: xamarin xamarin.forms xamarin.android

我正在开发Xamarin Forms(Forms版本:2.3.4.231)项目。 该应用程序在iOS中运行良好,而在Android中,我得到随机崩溃。从崩溃日志来看,这似乎是一次内部崩溃,但我不知道从哪里开始。

我知道我在应用中没有足够的信息。但有人可以帮助我更好地理解这个问题吗?

以下是崩溃日志:

    11/07/2017 PM 12:33:13.216: Cannot access a disposed object.
Object name: 'Xamarin.Forms.Platform.Android.FormsTextView'.  at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00030] in <b1a275385773449f855cd147e39ca2c4>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00001] in <b1a275385773449f855cd147e39ca2c4>:0 
  at Android.Widget.TextView.set_TextFormatted (Java.Lang.ICharSequence value) [0x00028] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at Android.Widget.TextView.set_Text (System.String value) [0x00013] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.UpdateText () [0x00098] in <1481f85a917c4f2b882ea161e9bc082f>:0 
  at Xamarin.Forms.Platform.Android.LabelRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x000c5] in <1481f85a917c4f2b882ea161e9bc082f>:0 
  at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00012] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x000fb] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0015b] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x001f9] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__47_0 () [0x00000] in <0f1ce60dbc6b4e24af01294c7acf41c6>:0 
  at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <37dcd078418a4ced956136dfaeb51ee2>:0 
  at (wrapper dynamic-method) System.Object:0cbcd7f2-854c-44ac-8467-eb8b42716bc1 (intptr,intptr)

2 个答案:

答案 0 :(得分:1)

这似乎是许多人遇到的问题(1234)。它可以在2.3.5中修复,因此请尝试安装2.3.5 XF库中的一个,看看问题是否消失。

还有一些答案here可能适合您。

  1. 创建Entry和/或Label渲染器,覆盖OnAttachedToWindowDispose并捕获异常。

  2. 尝试将ListView.CachingStrategy设置为RecycleElement

答案 1 :(得分:1)

不幸的是,我们在Xamarin.Forms 3.6.X上遇到了此错误。我不知道这可能在4.X中已修复,但是与此同时,我们能够通过创建自己的LabelRenderer(如@ hvaughan3建议的)来解决该问题。

要保存一些输入内容,请输入以下网址:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Label), typeof(Your.Namespace.Renderers.YourLabelRenderer))]
namespace Your.Namespace.Renderers
{
    public class YourLabelRenderer : LabelRenderer
    {
        public YourLabelRenderer(Android.Content.Context context) : base(context) { }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            try
            {
                base.OnElementPropertyChanged(sender, e);
            }
            catch (System.ObjectDisposedException)
            {
                //This is addressing a crash in Xamarin forums 3.6 
            }
        }
    }
}