对象引用未设置为对象的实例..添加Listview.Itemsource Xamarin

时间:2015-09-01 08:38:48

标签: c# xamarin observablecollection

我有这段代码:

[Table ("address_info")]
public class AddressInfoDB
{
    [PrimaryKey, Unique]
    public int stt { set; get; }
    public int customerAddressId { set; get; }
    public string address { set; get; }
    public string phone { set; get; }
    public int cityId { set;get; }
    public int districtId { set; get; }
    public int wardId { set; get; }
    public string cityName { set; get; }
    public string districtName { set; get; }
    public string wardName { set; get; }
    public string fullAddress { set; get; }
    public int isMainAddress { set; get; }      

    public override string ToString ()
    {
        return this.fullAddress;
    }
}

public class AddressSenderInList
{
    public ObservableCollection<AddressInfoDB> itemsList;
    public AddressSenderInList()
    {
        this.itemsList = new ObservableCollection<AddressInfoDB> ();
    }

}

以及从SQLite获取数据以填充内容页面中的列表视图的代码

listItems = new AddressSenderInList();

listView = new ListView {
    SeparatorVisibility = SeparatorVisibility.None,
    HasUnevenRows = true,
    ItemTemplate = new DataTemplate(() => new AddressInfoCell(this)),
    VerticalOptions = LayoutOptions.FillAndExpand,
    HorizontalOptions = LayoutOptions.Fill,
    IsPullToRefreshEnabled = true,
    ItemsSource = listItems.itemsList,
};

        fillListAddress ();

SQLite控制器中的代码GetAllAddressInfo

  public async Task<List<AddressInfoDB>> GetAllAddressInfo(){
        var lists = await dbConn.Table<AddressInfoDB> ().ToListAsync ();
        return lists;
  }

代码fillListAddress函数

public async void fillListAddress(){
    var abc = await App.dbManager.GetAllAddressInfo ();
    Debug.WriteLine ("Data from SQLite: "+abc.Count);
    if (listItems.itemsList == null) {
        Debug.WriteLine ("ItemSource Null?");
    } else {
        Debug.WriteLine ("ItemSource not null: "+listItems.itemsList.Count);
        foreach (var item in abc) {
            listItems.itemsList.Add (item);
        }
    }
}

但我收到错误

  

System.NullReferenceException:未将对象引用设置为实例   对象

我确信代码从SQLite获取数据是正确的。

输出打印:

Data from SQLite: 2
ItemSource not null: 0

0 个答案:

没有答案