空指针,尽管小部件初始化正确



我正在尝试填充列表视图。下面给出的是代码。我得到空指针异常。在调试时,我发现mListView为null。我不确定为什么即使列表初始化正确,mListview也是空的。

public class MainActivity : Activity 
{
    private IndexableListView mListView;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);
        List<string> mItems = new List<string> ();
        mItems.Add("Diary of a Wimpy Kid 6: Cabin Fever");
        mItems.Add("Steve Jobs");
        mItems.Add("Inheritance (The Inheritance Cycle)"); 
        ContentAdapter adapter = new ContentAdapter(this,         Android.Resource.Layout.SimpleListItem1, mItems);
        mListView = (IndexableListView) FindViewById<ListView>(Resource.Id.listview);
         mListView.Adapter = adapter; 

上面代码中我设置适配器的最后一行-在那里mListview为null,并给了我异常。非常感谢您的帮助。PS:以上代码适用于Xamarin

您的布局-Resource.Layout.Main不包含id为listviewListView

最新更新