【问题标题】:Clicking on the button crashes the application with unhandeled exception单击该按钮会使应用程序崩溃,并出现未处理的异常
【发布时间】:2012-10-31 20:12:09
【问题描述】:

我收到以下错误.....

追踪:

at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) 在 MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 在 /Users/ravichandra/Desktop/19-10-2012/F2S/F2S.UI/Main.cs:21 中的 F2S.UI.Application.Main (string[]) [0x00000] at (wrapper runtime-invoke) .runtime_invoke_void_object (object,intptr,intptr,intptr)

本机堆栈跟踪:

0   navigation                          0x00090dfc mono_handle_native_sigsegv + 284
1   navigation                          0x00005c28 mono_sigsegv_signal_handler + 248
2   libsystem_c.dylib                   0x9ca8259b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x0223555a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
5   UIKit                               0x022dab76 -[UIControl sendAction:to:forEvent:] + 66
6   UIKit                               0x022db03f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
7   UIKit                               0x022da2fe -[UIControl touchesEnded:withEvent:] + 549
8   UIKit                               0x0225aa30 -[UIWindow _sendTouchesForEvent:] + 513
9   UIKit                               0x0225ac56 -[UIWindow sendEvent:] + 273
10  UIKit                               0x02241384 -[UIApplication sendEvent:] + 464
11  UIKit                               0x02234aa9 _UIApplicationHandleEvent + 8196
12  GraphicsServices                    0x047f6fa9 PurpleEventCallback + 1274
13  CoreFoundation                      0x0119c1c5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
14  CoreFoundation                      0x01101022 __CFRunLoopDoSource1 + 146
15  CoreFoundation                      0x010ff90a __CFRunLoopRun + 2218
16  CoreFoundation                      0x010fedb4 CFRunLoopRunSpecific + 212
17  CoreFoundation                      0x010feccb CFRunLoopRunInMode + 123
18  GraphicsServices                    0x047f5879 GSEventRunModal + 207
19  GraphicsServices                    0x047f593e GSEventRun + 114
20  UIKit                               0x02232a9b UIApplicationMain + 1175
21  ???                                 0x0d90a384 0x0 + 227582852
22  ???                                 0x0d909180 0x0 + 227578240
23  ???                                 0x0d9089a0 0x0 + 227576224
24  ???                                 0x0d908a2f 0x0 + 227576367
25  navigation                          0x00009fe2 mono_jit_runtime_invoke + 722
26  navigation                          0x0016b87e mono_runtime_invoke + 126
27  navigation                          0x0016fa04 mono_runtime_exec_main + 420
28  navigation                          0x00174df5 mono_runtime_run_main + 725
29  navigation                          0x000671e5 mono_jit_exec + 149
30  navigation                          0x00203e41 main + 2209
31  navigation                          0x00002fe5 start + 53
32  ???                                 0x00000005 0x0 + 5

================================================ =================== 执行本机代码时获得 SIGSEGV。这通常表明 单声道运行时或本机库之一中的致命错误。

================================================ ===================

请任何人帮助我

这是我的代码

        btnMylocation.Clicked += (sender, e) => 
        {
            imgbg.Hidden=false;
            lbltime.Hidden=false;
            btnup.Hidden=false;
            btndown.Hidden=false;
            imghazard.Hidden=false;
            lblfeets.Hidden=false;


            try
            {
                if(!Reachability.IsHostReachable ("www.google.com"))
                {

                    alertview.NetworkFail ();

                }

                else
                {

                this.mysegments.SelectedSegment = 0;

                mapView.RemoveAnnotations (mapView.Annotations);

                var locationservice = new LocationService ();
                var currentLocation = locationservice.GetCurrentLocation ();
                var visregion = BuildVisibleRegion (currentLocation);

                mapView.SetRegion (visregion, true);
                targetimage.Hidden = true;

                locations = LocationManager.Locations ();

                List<double> distlist=new List<double>();

                foreach(LocationModel d in locations)
                {

                    var dist=distance (currentLocation.Latitude,currentLocation.Longitude,Convert.ToDouble (d.latitude),Convert.ToDouble (d.longitude));
                    distlist.Add (dist);

                }
                if(distlist.Count!=0)
                {

                    distlist.Sort ();
                    double min= distlist[0];

                foreach (LocationModel f in locations)
                {

                    string feets = (min*3280).ToString ();

                    var dist=distance (currentLocation.Latitude,currentLocation.Longitude,Convert.ToDouble (f.latitude),Convert.ToDouble (f.longitude));

                    if(min == dist)
                    {
                        string lat = f.latitude;
                        string lon = f.longitude;
                        NSUserDefaults.StandardUserDefaults.SetString (f.hazardid,"hazardid");  

                        this.View.AddSubview (imgbg);
                        this.View.AddSubview (imghazard);
                        this.View.AddSubview (lblfeets);
                        this.View.AddSubview (btnup);
                        this.View.AddSubview (btndown); 
                        this.View.AddSubview (lbltime);

                    if (f.hazardtype == 1) 
                    {   

                        pins.Clear ();
                        pins.Add (new PoliceAnnotation (new CLLocationCoordinate2D (Convert.ToDouble (lat), Convert.ToDouble (lon)), "Click here to Report",""));
                        mapView.AddAnnotationObject (pins [0]);
                        mapView.AddAnnotation (mapAnnotations [(int)Anotationindex.police]);

                        lblfeets.Text=feets  + " fts";
                        lbltime.Text=f.ReportDate + " Ago";
                        imghazard.Image=UIImage.FromFile ("Images/police-cop-mark.png");

                    } 

                    else if (f.hazardtype == 2) 
                    {

                        duipins.Clear ();
                        duipins.Add (new DuiAnnotation (new CLLocationCoordinate2D (Convert.ToDouble (lat), Convert.ToDouble (lon)), "Click here to Report",""));
                        mapView.AddAnnotationObject (duipins [0]);
                        mapView.AddAnnotation (mapAnnotations [(int)Anotationindex.dui]);


                        lblfeets.Text=feets  + " fts";
                        lbltime.Text=f.ReportDate + " Ago";
                        imghazard.Image=UIImage.FromFile ("Images/dui-stop-mark.png");      

                    }
                    else if(f.hazardtype==3)
                    {

                        campins.Clear ();
                        campins.Add (new SpeedcamAnnotation (new CLLocationCoordinate2D (Convert.ToDouble (lat), Convert.ToDouble (lon)), "",""));
                        mapView.AddAnnotationObject (campins [0]);
                        mapView.AddAnnotation (mapAnnotations [(int)Anotationindex.cam]);


                        lblfeets.Text=feets  + " fts";
                        lbltime.Text=f.ReportDate + " Ago";
                        imghazard.Image=UIImage.FromFile ("Images/speed-camera-mark.png");

                    }

                    }

                    }

                    btnup.TouchUpInside += (sender1, e1) =>
                    {

                    NSUserDefaults.StandardUserDefaults.SetString("1","Status");
                    NSUserDefaults.StandardUserDefaults.Init();
                    Response = LocationManager.ConfirmLocation();

                    if(Response.Status == "1" || Response.Status == "12")
                    {
                        UIAlertView alert1 = new UIAlertView ("Success","",null,"Ok");  
                        alert1.Show ();
                    }
                    else
                    {

                    }

                    };

                    btndown.TouchUpInside += (sender1, e1) => 
                    {

                    NSUserDefaults.StandardUserDefaults.SetString("0","Status");
                    NSUserDefaults.StandardUserDefaults.Init();
                    Response = LocationManager.ConfirmLocation ();

                    if(Response.Status == "1" || Response.Status == "12")
                    {
                        UIAlertView alert1 = new UIAlertView ("Success","",null,"Ok");  
                        alert1.Show ();
                    }
                    else
                    {

                    }

                    };

                   }

                }

               }
            catch(Exception ex)
            {

                if(ex.Message == "Network Fail")

                    alertview.NetworkFail ();

                else

                    alertview.ProcessFailed ();

                }

            };

第一次单击 btnMylocation 会显示一个带有两个按钮的弹出窗口,然后单击两个按钮工作正常,但是当我尝试再次单击 btnMylocation 时,它会打开弹出窗口,但单击 btnup 和 btndown 两个按钮中的任何一个按钮会使应用程序崩溃。 ......任何想法请......

【问题讨论】:

  • 看起来 GC 释放了按钮,因为没有人保留对它的引用。您能出示您的代码以便我们确定吗?

标签: c# xamarin.ios


【解决方案1】:

Rolf 是对的,有一种方法可以通过不正确地设置事件来使这种崩溃发生。这是 GC 与 MonoTouch 中的 Objective-C 交互的方式。

因此,例如,如果您将其放在名为 myButton 的插座上的 ViewDidLoad 中,它将正常工作:

myButton.TouchUpInside += (sender, e) =>
{
  //Do something
};

但如果你这样做:

myButton1.TouchUpInside += (sender, e) =>
{
  var myButton2 = new UIButton();
  myButton2.TouchUpInside += (sender, e) =>
  {
    //Do something
  };  
  //Add button2 to the view, etc.
};

然后myButton2 可能会在第一次正确单击,但最终会在 GC 收集后崩溃。发生这种情况是因为 myButton2 的 C# 包装器将被垃圾收集,但 Objective-C 实例仍将挂起。当Objective-C实例被点击时,它不能调用C#,因为它已经消失了。

一种简单的解决方法,使按钮成为类的成员变量:

private UIButton myButton;

只要确保不要一遍又一遍地创建它。

【讨论】:

  • 我同意解决方法 - 但在您说明的情况下,崩溃是一个 MT 错误,不是吗?我不认为 myButton2 在这种情况下应该收集垃圾,不是吗? (只是检查我明白了!)
  • 不是真正的错误,而是限制。我不确定他们能做些什么来解决它。它现在的工作方式,如果一个 C# 包装对象被释放/GC'd,如果它的引用计数为零,它将释放它各自的 Objective-C 对象——这就是内存管理在 Objective-C 中的工作方式。但在这种情况下,引用计数不为零,因为它仍在屏幕上和子视图的集合中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多