System.InvalidCastException - 工程调试,失败在发布 [英] System.InvalidCastException - works in debug, fails at release

查看:125
本文介绍了System.InvalidCastException - 工程调试,失败在发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
06-11 19:39:01.690 I/MonoDroid(17577):   at MyApp.Inbox.CorrespondenceActivity.CorrectLinkPaths (ISpanned spanText) [0x00000] in <filename unknown>:0 

这是我收到的例外。在建设有调试配置应用程序,一切工作正常的 - 没有例外,什么都没有。有一次,我建立它发布,我得到这个例外 - 我想不通为什么,以及如何解决它。

That is the exception I'm getting. When building the application with Debug configuration, everything works alright - no exceptions, no nothing. Once I build it for release, I get this exception - and I can't figure out why, and how to fix it.

这是code调用方法CorrectLinkPaths:

This is the code that invokes the method CorrectLinkPaths:

_someTextView.SetText(CorrectLinkPaths(Html.FromHtml(_myMessage.Summary + "\n\n" + _myMessage.Body)), TextView.BufferType.Spannable);

不用多说,_myMessage.Summary美体是HTML串。

Needless to say, _myMessage.Summary and Body are HTML-strings.

我CorrectLinkPaths法看起来是这样的:

My CorrectLinkPaths-method looks like this:

private ISpanned CorrectLinkPaths(ISpanned spanText)
    {
        Object[] spans = spanText.GetSpans(0, spanText.Length(), Class.FromType(typeof (Object)));
        foreach (var s in spans)
        {
            int start = spanText.GetSpanStart(s);
            int end = spanText.GetSpanEnd(s);
            SpanTypes flags = spanText.GetSpanFlags(s);
            if (s.GetType() == typeof (URLSpan))
            {
                var urlSpan = (URLSpan) s;
                if (!urlSpan.URL.StartsWith("http"))
                {
                    if (urlSpan.URL.StartsWith("/"))
                        urlSpan = new URLSpan("http://www.mydomain.com" + urlSpan.URL);
                    else
                        urlSpan = new URLSpan("http://www.mydomain.com/" + urlSpan.URL);
                }
                ((ISpannable) spanText).RemoveSpan(s);
                ((ISpannable) spanText).SetSpan(urlSpan, start, end, flags);
            }
        }
        return spanText;
    }

没有人有任何线索,为什么调试期间工作的,但是当我尝试建立发布,以及如何解决它失败?我会非常,非常,非常感谢!

Does anyone have any clue as to why this works during debug, but fails when I try to build for release, and how to fix it? I would be very, very, very grateful!

感谢您:)

最好的问候。

推荐答案

解决方案是相当简单的。它被所述的方法的,导致错误的底部铸造

The solution was fairly simple. It was the casting at the bottom of the method that resulted in the error

((ISpannable) spanText).RemoveSpan(s);
((ISpannable) spanText).SetSpan(urlSpan, start, end, flags); 

要解决此方法必须改变为以下内容:

To work around this the method had to be changed to the following:

public ISpanned CorrectLinkPaths(ISpanned spanText)
{
    Object[] spans = spanText.GetSpans(0, spanText.Length(), Class.FromType(typeof (Object)));
    ISpannable spanned = new SpannableString(spanText);
    foreach (var s in spans)
    {
        var start = spanText.GetSpanStart(s);
        var end = spanText.GetSpanEnd(s);
        var flags = spanText.GetSpanFlags(s);

        if (s.GetType() == typeof (URLSpan))
        {
            var urlSpan = (URLSpan)s;
            if (!urlSpan.URL.StartsWith("http"))
            {
                if (urlSpan.URL.StartsWith("/"))
                    urlSpan = new URLSpan("http://www.mydomain.com" + urlSpan.URL);
                else
                    urlSpan = new URLSpan("http://www.mydomain.com/" + urlSpan.URL);
            }
            spanned.RemoveSpan(s);
            spanned.SetSpan(urlSpan, start, end, flags);
        }
    }
    return spanned;
}

这篇关于System.InvalidCastException - 工程调试,失败在发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆