- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
This问题是一样的,但它似乎没有回答为什么这个转换在 Release模式下失败但在调试中工作的问题。
Android 文档指定:
Return the text the TextView is displaying. If setText() was called with an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast the return value from this method to Spannable or Editable, respectively. Note: The content of the return value should not be modified. If you want a modifiable one, you should make your own copy first.
如果我在调试中运行以下命令它会工作,在发布时它会抛出一个 InvalidCastException
var editText = FindViewById<EditText>(Resource.Id.MyEditText);
editText.SetText("hello", TextView.BufferType.Spannable);
var myTextView = FindViewById<TextView>(Resource.Id.MyTextView);
try
{
ISpannable t21 = (ISpannable)editText.TextFormatted;
ISpanned t22 = (ISpanned)editText.TextFormatted;
}
catch (Exception exception)
{
myTextView.Text = exception.Message;
}
FATAL EXCEPTION: main
06-09 16:30:34.135 E/AndroidRuntime(31672): Process: App27.App27, PID: 31672
06-09 16:30:34.135 E/AndroidRuntime(31672): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
06-09 16:30:34.135 E/AndroidRuntime(31672): Caused by: md52ce486a14f4bc06-09 16:30:34.135 E/AndroidRuntime(31672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-09 16:30:34.135 E/AndroidRuntime(31672): at dalvik.system.NativeStart.main(Native Method)
06-09 16:30:34.135 E/AndroidRuntime(31672): Caused by: java.lang.reflect.InvocationTargetException
06-09 16:30:34.135 E/AndroidRuntime(31672): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 16:30:34.135 E/AndroidRuntime(31672): at java.lang.reflect.Method.invoke(Method.java:515)
06-09 16:30:34.135 E/AndroidRuntime(31672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-09 16:30:34.135 E/AndroidRuntime(31672): ... 2 more
06-09 16:30:34.135 E/AndroidRuntime(31672): Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.InvalidCastException: Cannot cast from source type to destination type.
06-09 16:30:34.135 E/AndroidRuntime(31672): at App27.MainActivity.OnCreate (Android.OS.Bundle) [0x00074] in d:\Users\dbeattie\Documents\Visual Studio 2013\Projects\App27\App27\MainActivity.cs:29
06-09 16:30:34.135 E/AndroidRuntime(31672): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00013, 0x000ef>
06-09 16:30:34.135 E/AndroidRuntime(31672): at (wrapper dynamic-method) object.6917b467-8852-465b-9332-eaefa6fe6832 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
版本信息:
Xamarin 3.11.590.0 (5160db7) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.
Xamarin.Android 5.1.3.1 (d419c934e6ce2113653ff4c40214e3a5d5a69440) Visual Studio plugin to enable development for Xamarin.Android.
最佳答案
我能够重现您的问题。虽然我不能确切地告诉你为什么会发生这种情况,但我确实注意到了这一点。这可能是 Xamarin.Android 中的一个错误和/或链接器过于激进并且做了一些导致 InvalidCastException 的事情,这就是我所做的。
在我的发布配置中,我的链接器设置为“仅限 SDK 程序集”。 InvalidCastException 发生了。当我将链接器设置为“不链接”时,InvalidCastException 没有发生。这使其类似于链接器设置为“不链接”的调试配置。
因此,链接器似乎正在剥离所需的内容和/或 Xamarin.Android 存在错误。
但是,我确实找到了一个既适用于调试又适用于发布的解决方案。由于 ISpannable 和 ISpanned 对象是 Java 对象的 java 桥梁,最终将实现 Java.Lang.Obj,因此我通常在转换这些对象时使用 JavaCast<>。在转换 C# 对象时,我使用 () 或“as”关键字。在这种情况下,由于您正在尝试转换 Java 对象(包装器),因此正确的转换方法应该是使用 JavaCast<>,如下所示:
var editText = FindViewById<EditText>(Resource.Id.MyEditText);
editText.SetText("hello", TextView.BufferType.Spannable);
var myTextView = FindViewById<TextView>(Resource.Id.MyTextView);
try
{
ISpannable t21 = editText.TextFormatted.JavaCast<ISpannable>();
ISpanned t22 = editText.TextFormatted.JavaCast<ISpanned>();
}
catch (Exception exception)
{
myTextView.Text = exception.Message;
}
使用此方法适用于调试和发布配置,包括将链接器设置为“不链接”和“仅 SDK 程序集”。
无论哪种方式,我都可能会通过在 http://bugzilla.xamarin.com 上提交错误报告让 Xamarin 人员知道。 .无论如何,我认为在这种情况下使用 JavaCast<>(因为您正在转换 java 包装器)是在这种情况下转换的正确方法。
关于c# - 将 TextFormatted 转换为 ISpannable 或 ISpanned 时发布的 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742875/
所以我有一个名为 OutputInformation 的类,我想存储它然后在另一台计算机上读取以检索数据。 我正在使用二进制序列化。 [Serializable()] public class Out
我一直在使用 Visual Studio 2010、C#。 我从事这个项目已经将近一年了。在其中,我使用存储在单独的 XML 文件中的信息来构建图像。这个过程已经为我工作了数百次,没有任何问题。 我已
我得到一个 InvalidCastException我不明白为什么。 这是引发异常的代码: public static void AddToTriedList(string recipeID) {
我有一个执行数据转储的 exe。 exe 将根据配置动态获取 DLL,并将类对象传递给它。 DLL 有一个用它编译的此类的副本,并且可以在调试下看到数据,没有问题作为对象。但是,当我尝试将它转换到类里
我基本上想做的是将从 Web 服务获取的对象转换为与我的 Entity Framework 对象兼容的对象。我虽然通过创建一个同时应用于 Web 服务对象和 EF 对象的接口(interface)来做
我有一个查询,其结果如下所示 Blue OrangeRed Black Green Silver Red 我打算从这个结果集中创建一个 System.Drawing.Color 对象的列表。当我运行该
我有一个 GridView ,当双击一条记录时,我希望它为该特定记录打开一个新的详细 View 表单。 例如,我创建了一个 Customer 类: using System; using System
我目前对我遇到的问题感到非常困惑。我正在为另一个提供公共(public) .NET API 的应用程序编写插件。我创建了一个名为 Room 的类,我使用 PropertyGrid 允许用户查看和编辑
我在具有自定义类的字典和列表中遇到了该异常。示例: List dsa = (List)Session["Display"]; 当我使用 Session 时,转换工作了 10-20 次..然后它开始抛
我在 Entity Framework Core 1.1 中遇到此查询的问题: var leaves = _context.Proposal.OfType() .Inc
当我在 NET Framework 4.7.2 中创建新的 WPF 应用程序时,将菜单添加到 MainWindow 时,每当您单击菜单然后单击菜单以外的位置将其关闭时,我都会收到 InvalidCas
我的一个程序集包含以下“提供者”类型: 我还有一个 XML 文件,它使用 DeviceInfoProvider 基类保存提供者信息。简化版本如下所示: 在运行时,我将 XML 字段映射到我的变
我有一个扩展方法,我在 WinForms 中使用了多年,但自从我尝试在新的 WPF 项目中使用它后就没有了。方法: public static String GetDescription(this E
我在以下代码的运行时收到“InvalidCastException”: 我的 C# WinForm 代码包含一个组合框,它使用以下代码从数据库中填充: public void PopulateComp
下面的代码给了我一个 InvalidCastException,说明我不能在 foreach 循环中从源类型转换到目标类型。我尝试通过该方法传递多个不同的通用集合,但我总是收到此错误。我不知道为什么。
(问题的真正标题应该是“为什么我得到一个‘无法转换类型为‘System.Runtime.CompilerServices.TaskAwaiter`1[System.Runtime.CompilerSe
我在 C# 中有以下代码片段 List list = new List() { 1, 23, 5, 3, 423, 3 }; var query = list.Cast().S
这个特定问题更多的是试图找出编译器消息背后的原因,而不是解决问题,但我希望它没问题。 假设我们有: class Foo { public static explicit operator Fo
我想统计某个时间段之间的记录数。 我使用 Npgsql 准备查询,然后添加变量部分(出于安全原因): lCommand.CommandText = "select count(*) from
Closed. This question is off-topic。它当前不接受答案。
我是一名优秀的程序员,十分优秀!