xamarin按钮里面的片段返回null异常

时间:2016-09-05 15:55:37

标签: android button xamarin android-viewpager fragment

我是使用xamarin开发移动应用程序的新手,我在ViewPager中的按钮中添加功能时遇到了问题。它总是在Button.click上返回null异常。有人可以帮忙吗在此先感谢:)

这是我的代码〜

namespace SlidingTabLayoutTutorial
{
    public class SlidingTabsFragment : Fragment
    {
        private SlidingTabScrollView mSlidingTabScrollView;
        private ViewPager mViewPager;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.fragment_sample, container, false);
            Button btn = view.FindViewById<Button>(Resource.Id.button1);
            btn.Click += btn_Click;
            return view;
        }

        void btn_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            mSlidingTabScrollView = view.FindViewById<SlidingTabScrollView>(Resource.Id.sliding_tabs);
            mViewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
            mViewPager.Adapter = new SamplePagerAdapter();
            mSlidingTabScrollView.ViewPager = mViewPager;
        }

        public class SamplePagerAdapter : PagerAdapter
        {
            List<string> items = new List<string>();

            public SamplePagerAdapter() : base()
            {
                items.Add("Profile");
                items.Add("Reservation");
                items.Add("Notifications");
            }

            public override int Count
            {
                get { return items.Count; }
            }

            public override bool IsViewFromObject(View view, Java.Lang.Object obj)
            {
                return view == obj;
            }
            public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
            {
                if (position == 0)
                {
                    View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.profileLayout, container, false);
                    container.AddView(view);
                    return view;
                }
                else
                {
                    return position;
                }
            }

            public string GetHeaderTitle(int position)
            {
                return items[position];
            }

            public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object obj)
            {
                container.RemoveView((View)obj);
            }
        }
    }
}

0 个答案:

没有答案