Xamarin.iOS随机崩溃无效的强制转换异常

时间:2014-03-17 22:33:31

标签: ios casting xamarin.ios crash xamarin

我的应用程序中有一些奇怪的崩溃,我试图跟踪问题但是随机发生并且在不同的clases或方法中,所有崩溃都是无效的强制转换异常但是没有逻辑...这是一个他们

//ContactsViewController.cs
//Initialize Components

tableContact=IOTM.GUI.CreateTable(rTable,UIColor.White,UIColor.White,50f);
refresh = IOTM.GUI.createRefreshControl (tableContact);
refresh.ValueChanged += reloadData;
//IOTM.GUI
public static UIRefreshControl createRefreshControl(UITableView table){
        UIRefreshControl refresh=new UIRefreshControl ();
        var firstAttrib = new UIStringAttributes {
            ForegroundColor = UIColor.Black,
            BackgroundColor = UIColor.Clear,
            Font = UIFont.FromName("Helvetica", 10f )
        };

        string registerString = "Pull to Update Contacts";
        var styledRegisterString = new NSMutableAttributedString (registerString);

        styledRegisterString.AddAttributes(firstAttrib, new NSRange (0,registerString.Length) );
        refresh.AttributedTitle = styledRegisterString;
        refresh.TintColor = ICGlobal.OrangeLigth;

        table.AddSubview (refresh);

        return refresh;
    }
public static UITableView CreateTable( RectangleF bounds, UIColor bgColor, UIColor lineColor, float rowHeight, UIView v = null)
    {
        UITableView t = new UITableView (bounds) {
            BackgroundColor = bgColor,
            SeparatorColor = lineColor,
            RowHeight = rowHeight
        };

        if (null != v) {
            v.AddSubview (t);
        }
        return t;
    }

这是错误日志

Unhandled Exception:
0   CardReader_iOS                      0x004188a3 mono_handle_exception_internal + 2022
1   CardReader_iOS                      0x004180b7 mono_handle_exception + 10
2   CardReader_iOS                      0x004128dd mono_arm_throw_exception + 104
3   CardReader_iOS                      0x002e6b9c throw_exception + 68
at MonoTouch.Foundation.NSArray.UnsafeGetItem<T> (intptr,uint) <0x00044>
at MonoTouch.Foundation.NSArray.ArrayFromHandle<T> (intptr) <0x0008c>
at MonoTouch.UIKit.UIView.get_Subviews () [0x00010] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIView.g.cs:2402
at MonoTouch.UIKit.UIView.AddSubview (MonoTouch.UIKit.UIView) [0x0005c] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIView.g.cs:475
at Core_IC_IOTM.GUI.createRefreshControl (MonoTouch.UIKit.UITableView) <IL 0x00075, 0x00368>
at CardReader_iOS.ContactsViewController.InitializeComponents () <IL 0x00368, 0x01924>
at CardReader_iOS.ICBaseController.ViewDidLoad () <IL 0x00018, 0x000d4>
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0x000c8>
12  CardReader_iOS                      0x004243f5 mono_jit_runtime_invoke + 1196
13  CardReader_iOS                      0x0046059d mono_runtime_invoke + 88
14  CardReader_iOS                      0x003f4e69 native_to_managed_trampoline_CardReader_iOS_ICBaseController_ViewDidLoad + 232
15  UIKit                               0x30e224cb <redacted> + 518
16  UIKit                               0x30e22289 <redacted> + 24
17  UIKit                               0x30fae38b <redacted> + 634
18  UIKit                               0x30ecbd83 <redacted> + 418
19  UIKit                               0x30ecbb8d <redacted> + 44
20  UIKit                               0x30ecbb25 <redacted> + 184
21  UIKit                               0x30e1dd79 <redacted> + 380
22  QuartzCore                          0x30a9b62b <redacted> + 142
23  QuartzCore                          0x30a96e3b <redacted> + 350
24  QuartzCore                          0x30a96ccd <redacted> + 16
25  QuartzCore                          0x30a966df <redacted> + 230
26  QuartzCore                          0x30a964ef <redacted> + 314
27  UIKit                               0x30e21401 <redacted> + 8232
28  CoreFoundation                      0x2e5cd25b <redacted> + 14
29  CoreFoundation                      0x2e5cc72b <redacted> + 206
30  CoreFoundation                      0x2e5caf1f <redacted> + 622
31  CoreFoundation                      0x2e535f4f CFRunLoopRunSpecific + 522
32  CoreFoundation                      0x2e535d33 CFRunLoopRunInMode + 106
33  GraphicsServices                    0x3343a663 GSEventRunModal + 138
34  UIKit                               0x30e8116d UIApplicationMain + 1136
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00110>
at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38
at CardReader_iOS.Application.Main (string[]) <IL 0x0000c, 0x0007c>
at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0x000c8>
39  CardReader_iOS                      0x004243f5 mono_jit_runtime_invoke + 1196
40  CardReader_iOS                      0x0046059d mono_runtime_invoke + 88
41  CardReader_iOS                      0x00464177 mono_runtime_exec_main + 282
42  CardReader_iOS                      0x00463fb7 mono_runtime_run_main + 474
43  CardReader_iOS                      0x004126e5 mono_jit_exec + 48
44  CardReader_iOS                      0x004b0fe8 main + 2464
45  libdyld.dylib                       0x392bbab7 <redacted> + 2

Mar 17 17:33:19 iPhone-de-Hector CardReader_iOS[4306] <Warning>: Unhandled managed exception: Unable to cast object of type 'MonoTouch.UIKit.UIScrollView+_UIScrollViewDelegate' to type 'MonoTouch.UIKit.UIView'. (System.InvalidCastException)
  at MonoTouch.ObjCRuntime.Runtime.GetINativeObject[UIView] (IntPtr ptr, Boolean owns) [0x00000] in <filename unknown>:0 
  at MonoTouch.Foundation.NSArray.UnsafeGetItem[UIView] (IntPtr handle, UInt32 index) [0x00000] in <filename unknown>:0 
  at MonoTouch.Foundation.NSArray.ArrayFromHandle[UIView] (IntPtr handle) [0x00000] in <filename unknown>:0 
  at MonoTouch.UIKit.UIView.get_Subviews () [0x00010] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIView.g.cs:2402 
  at MonoTouch.UIKit.UIView.AddSubview (MonoTouch.UIKit.UIView view) [0x0005c] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIView.g.cs:475 
  at Core_IC_IOTM.GUI.createRefreshControl (MonoTouch.UIKit.UITableView table) [0x00000] in <filename unknown>:0 
  at CardReader_iOS.ContactsViewController.InitializeComponents () [0x00000] in <filename unknown>:0 
  at CardReader_iOS.ICBaseController.ViewDidLoad () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 
  at CardReader_iOS.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 

1 个答案:

答案 0 :(得分:0)

我认为特定错误是由错误使用UIRefreshControl引起的。

https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIRefreshControl_class/Reference/Reference.html

  

注意:因为刷新控件是专门为使用而设计的   由表视图控制器管理的表视图,在a中使用它   不同的上下文可能会导致未定义的行为。

确保将控件分配给UITableViewController而不是作为子视图添加到UITableView:

  

除了为表视图分配刷新控件外   controller的refreshControl属性

这样的事情:

public class TableViewController : UITableViewController
{
    public TableViewController () : base (UITableViewStyle.Grouped)
    {
        this.RefreshControl = CreateRefreshControl ();
    }

    public static UIRefreshControl CreateRefreshControl()
    {

        var firstAttrib = new UIStringAttributes {
            ForegroundColor = UIColor.Black,
            BackgroundColor = UIColor.Clear,
            Font = UIFont.FromName("Helvetica", 10f )
        };

        string registerString = "Pull to Update Contacts";
        var styledRegisterString = new NSMutableAttributedString (registerString);

        styledRegisterString.AddAttributes(firstAttrib, new NSRange (0,registerString.Length) );
        return new UIRefreshControl () {
            AttributedTitle = styledRegisterString,
            TintColor = UIColor.Orange//ICGlobal.OrangeLigth
        };
    }