Xamarin ios应用程序中的空引用

时间:2016-12-07 07:53:42

标签: ios nullpointerexception xamarin.ios

我正在使用Xamarin iOS中的抓取数据创建TableView。我使用的守则如下。

代码:

public partial class SearchViewController : UIViewController
    {
        private SearchFilter searchFilter;
        List<SearchItem> searchItem;
        SearchTableSourceClass searchTablesourceclass;

        UITableView searchTableView;

        static NSString TextCellId = new NSString("TextCell");

        int Skip = 0, Take = 10;

        public SearchViewController(IntPtr handle) : base(handle)
        {

        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.Title = "Search";
            this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes() { ForegroundColor = UIColor.White };

            this.NavigationController.NavigationBar.BarTintColor = UIColor.Clear.FromHexString("#0072BA", 1.0f);
            this.NavigationController.NavigationBarHidden = false;

            btn_filter.TintColor = UIColor.White;

            searchTableView = new UITableView
            {
                Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height)
            };

            View.AddSubview(searchTableView);

            FnBindData();
        }

        async void FnBindData()
        {
            searchFilter = new SearchFilter(); ;
            searchFilter.Speciality = "Physician";
            searchFilter.Speciality = "";
            searchFilter.Days = new List<string>();
            searchFilter.Days.Add("Monday");
            searchFilter.Days.Add("TuesDay");
            searchFilter.Days.Add("Wednesday");
            searchFilter.Days.Add("Thursday");
            searchFilter.Days.Add("Friday");
            searchFilter.Days.Add("Saturday");
            searchFilter.Days.Add("Sunday");

            searchFilter.Area = "";
            searchFilter.City = "Surat";
            searchFilter.MaxFees = 500.0;
            searchFilter.FromTime = 570;
            searchFilter.ToTime = 750;

            searchItem = new List<SearchItem>();

            var json = JsonConvert.SerializeObject(searchFilter);
            searchItem = await SearchServices.GetSearchItem(Take, Skip, json);

            if (searchItem != null)
            {
                searchTablesourceclass = new SearchTableSourceClass(searchItem);
                searchTableView.RegisterClassForCellReuse(typeof(TextCell),TextCell.CellId);
                searchTableView.Source = searchTablesourceclass;
            }
        }
    }

    class SearchTableSourceClass : UITableViewSource
    {
        List<SearchItem> searchItem;

        public SearchTableSourceClass(List<SearchItem> searchItems)
        {
            this.searchItem = searchItems;
        }
        public override  UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = (TextCell)tableView.DequeueReusableCell(TextCell.CellId, indexPath);
            var item = searchItem[indexPath.Row];

            if (cell == null)
            {
                cell = (Gargi.iOS.TextCell)new UITableViewCell(UITableViewCellStyle.Default, TextCell.CellId);
            }

            cell.Hospital = item.Organization;

            return cell;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return searchItem.Count;
        }
    }

    class TextCell : UITableViewCell
    {
        UILabel hospitalLabel;
        public static readonly NSString CellId = new NSString("TextCell");

        public TextCell(IntPtr ptr) : base(ptr)
        {

        }

        public string Hospital
        {
            get
            {
                return hospitalLabel.Text;
            }
            set
            {
                hospitalLabel.Text = value;
                hospitalLabel.TextColor = UIColor.Black;
                SetNeedsDisplay();
            }
        }

        [Export("initWithFrame:")]
        TextCell(RectangleF frame) : base(frame)
        {
            hospitalLabel = new UILabel()
            {
                Frame = new CoreGraphics.CGRect(0, 0, ContentView.Frame.Size.Width - 70, 20),
                TextColor = UIColor.Black,
                AdjustsFontSizeToFitWidth = true,
                Font = UIFont.FromName("Helvetica-Bold", 14f),
                TextAlignment = UITextAlignment.Left
            };

            ContentView.AddSubview(hospitalLabel);
        }


    }

但是在本节中调试应用程序

set
        {
            hospitalLabel.Text = value;
            hospitalLabel.TextColor = UIColor.Black;
            SetNeedsDisplay();
        }

它给出了以下错误:

  

System.NullReferenceException:未将对象引用设置为实例   Gargi.iOS.TextCell.set_Hospital中的一个对象(System.String   值)[0x00008] in   /Users/BizSalt/Projects/Gargi/Gargi.iOS/SearchViewController.cs:131
  在Gargi.iOS.SearchTableSourceClass.GetCell(UIKit.UITableView   tableView,Foundation.NSIndexPath indexPath)[0x00046] in   /Users/BizSalt/Projects/Gargi/Gargi.iOS/SearchViewController.cs:102
  at at(包装器管理到原生)   UIKit.UIApplication:UIApplicationMain(int,string [],intptr,intptr)
  在UIKit.UIApplication.Main(System.String [] args,System.IntPtr   principal,System.IntPtr委托)[0x00005] in   /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:79   在UIKit.UIApplication.Main(System.String [] args,System.String   principalClassName,System.String delegateClassName)[0x00038] in   /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:63   在Gargi.iOS.Application.Main(System.String [] args)[0x00008]中   /Users/BizSalt/Projects/Gargi/Gargi.iOS/Main.cs:12

1 个答案:

答案 0 :(得分:0)

问题出在GetCell方法中。当您致电DequeueReusableCellnew UITableViewCell(UITableViewCellStyle.Default, TextCell.CellId)时,initWithFrame构造函数永远不会触发,因此当您尝试将值设置为Hospital对象时,它会尝试将值设置为hospitalLabel一片空白。 您必须向TextCell添加新的构造函数:

    public TextCell(UITableViewCellStyle style, NSString reuseIdentifier)
    {
        hospitalLabel = new UILabel()
        {
            Frame = new CoreGraphics.CGRect(0, 0, ContentView.Frame.Size.Width - 70, 20),
            TextColor = UIColor.Black,
            AdjustsFontSizeToFitWidth = true,
            Font = UIFont.FromName("Helvetica-Bold", 14f),
            TextAlignment = UITextAlignment.Left
        };

        ContentView.AddSubview(hospitalLabel);
    }