使用Visual Studio中的Xamarin调试正在运行的Android应用程序

5
当应用程序启动时,如果离线,我会从本地存储加载列表;如果在线,则从 Web 服务加载此列表(然后写入本地存储)。
我的问题是,为了调试此问题,我需要完全关闭应用程序,然后重新启动它(因此结束 Visual Studio 调试会话)。
应用程序将启动,但在我再次调用特定方法时会崩溃。我无法找出它在哪里出错。由于调试会话已不再连接,因此也没有任何帮助。
奇怪的是。我甚至尝试将方法调用和方法内容包装在 try catch 块中(以尝试弄清楚问题所在),但它仍然崩溃。
以下是代码片段。
 class MyCouncilsFragment:Fragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //my councils
        base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.View ?? inflater.Inflate(Resource.Layout.mycouncils, container, false);
        string councils = GoIO.LoadText(CC.FILE_MYCOUNCILS) ?? "";
        List<Council> mycouncils = Shared.Serialization.Deserialize<List<Shared.Council>>(councils) ?? new List<Council>();
        MyGlobals.myCouncilsList = mycouncils;


        Spinner spncouncils = view.FindViewById<Spinner>(Resource.Id.spnCouncils);           

        if(mycouncils.Count > 0)
        {
            spncouncils.Enabled = true;
            CouncilSpinnerAdapter adapter = new CouncilSpinnerAdapter(Main_Act.context, Resource.Layout.spn_row_text, MyGlobals.myCouncilsList.ToArray());
            spncouncils.Adapter = adapter;

            Button btnSelect = view.FindViewById<Button>(Resource.Id.btnSelect);

            btnSelect.Enabled = true;
            btnSelect.Click += delegate
            {                   
                Council sessionCouncil = MyGlobals.myCouncilsList[spncouncils.SelectedItemPosition];
                //sessionCouncil is not the problem, i've tested that

                try
                {
                    Main_Act.loadLists();
                    //the issue is when this method is called. (however the call works fine when calling it from a different fragment thats basically in the same state)
                }
                catch
                {                        
                }
                //List<Designer> x = Shared.WebServerHelper.WebCall<Designer>("https://" + MyGlobals.sessionCouncil.service_url + "/design/");
                //MyGlobals.sessionColours = x[0];
                //MyGlobals.hintText = MyGlobals.getSubtleColour(MyGlobals.getColor(MyGlobals.sessionColours.controlcolour), 20);

                //var intent = new Intent(this.Activity, typeof(MyDetails_Act));
                //StartActivity(intent);                    
            };

        }
        else
        {
            //List<Council> emptylist = new List<Council>();

            //Council empty = new Council();
            //empty.council_name = "no councils added";
            //emptylist.Add(empty);

            //spncouncils.Enabled = false;

            //Button viewall = view.FindViewById<Button>(Resource.Id.btnSelect);
            //viewall.Visibility = ViewStates.Gone;

            //TextView nb = view.FindViewById<TextView>(Resource.Id.nb);
            //nb.Visibility = ViewStates.Visible;


        }



        return view;
    }

这是被调用的方法

public static void loadLists()
    {
        try
        {
            //        //  _|          _|_|      _|_|    _|_|_|    
            //        //  _|        _|    _|  _|    _|  _|    _|  
            //        //  _|        _|    _|  _|_|_|_|  _|    _|  
            //        //  _|        _|    _|  _|    _|  _|    _|  
            //        //  _|_|_|_|    _|_|    _|    _|  _|_|_|  


            if (MyGlobals.sessionCouncil != null)
            {
                var connectivityManager = (ConnectivityManager)context.GetSystemService(ConnectivityService);
                var activeConnection = connectivityManager.ActiveNetworkInfo;

                if (activeConnection != null && activeConnection.IsConnected)
                {
                    loadConsentsFromWeb();
                    Console.WriteLine("getting inspectiontypes online");
                    loadInspectionTypesOnline();
                }
                else
                {
                    try
                    {
                        loadConsentsFromFile();
                        loadInspectionTypesOffline();
                    }
                    catch
                    {
                        Toast.MakeText(context, "Unable to load consents from file", ToastLength.Long).Show();

                    }

                }

                try
                {
                    loadBookings();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            else
            {
                //inspection types failed to load from file or from web. propmt user to reaload app? or something?           
                Toast.MakeText(context, "Definition Elements failed to load, please restart the app", ToastLength.Short).Show();

                System.Environment.Exit(0);
            }
        }
        catch
        {                
        }
    }

注意,我尝试将整个方法放入try块中,但应用程序仍然崩溃。


1
当您尝试调试时,输出中是否有某种错误消息?您是否也尝试打开Android设备监视器(DDMS)并查看来自logcat的输出? - arnfada
1
附加全局异常处理程序并在回调中设置断点,这将让您深入了解正在发生的情况:https://dev59.com/dV7Va4cB1Zd3GeqPM8eK#11450361 - matthewrdev
同时,请附上崩溃的logcat。 - matthewrdev
1个回答

1
尝试从命令提示符中运行adb logcat,并在应用程序崩溃时搜索致命异常。我没有足够的积分发表评论,所以我使用答案代替。抱歉 =)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接