Question

I'm having some strange crashes in my app, I have tried to track the issue but happens randomly and in different clases or methods, all crashes are invalid cast exception but there is no logic in that... this is one of them

//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;
    }

And this is the error log

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 
Was it helpful?

Solution

I think that particular error is caused by the UIRefreshControl being used incorrectly.

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

Note: Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behavior.

Make sure you assign the control to UITableViewController instead of adding as a subview to UITableView:

In addition to assigning a refresh control to a table view controller’s refreshControl property

Something like this:

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
        };
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top