按钮在Fragment中不起作用

时间:2016-07-25 12:02:15

标签: android android-fragments button android-fragmentactivity

所以我的问题是我的片段中包含的按钮会引发错误"未处理的异常 - 对象引用未设置为对象的实例" 我想是因为我引用了包含按钮的布局,这不会导致错误。如果有人能够解释我做错了什么就会很好。我想我要么缺少一些重要的代码,要么完全搞砸了,因为我不熟悉使用片段并理解它们与活动的不同之处。

这是我的MainActivity:

public class MainActivity : ActionBarActivity
{
    private SupportToolbar mToolbar;
    private MyActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ListView mLeftDrawer;

    private HomeFragment mHomeFragment;
    private LogInFragment mLogInFragment;
    private MatchCentreFragment mMatchCentreFragment;
    private PrevCompFragment mPrevCompFragment;
    private PrevFixFragment mPrevFixFragment;
    private SettingsFragment mSettingsFragment;
    private SocialFragment mSocialFragment;
    private UpcomCompFragment mUpcomCompFragment;
    private UpcomFixFragment mUpcomFixFragment;

    private SupportFragment mCurrentFragment = new SupportFragment();
    private Stack<SupportFragment> mStackFragments;

    private ArrayAdapter mLeftAdapter;

    private List<string> mLeftDataItems;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
        mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer);

        mHomeFragment = new HomeFragment();
        mLogInFragment = new LogInFragment();
        mMatchCentreFragment = new MatchCentreFragment();
        mPrevCompFragment = new PrevCompFragment();
        mPrevFixFragment = new PrevFixFragment();
        mSettingsFragment = new SettingsFragment();
        mSocialFragment = new SocialFragment();
        mUpcomCompFragment = new UpcomCompFragment();
        mUpcomFixFragment = new UpcomFixFragment();

        mStackFragments = new Stack<SupportFragment>();

        mLeftDrawer.Tag = 0;

        SetSupportActionBar(mToolbar);


        mLeftDataItems = new List<string>();
        mLeftDataItems.Add("Home");
        mLeftDataItems.Add("Log In");
        mLeftDataItems.Add("Match Centre");
        mLeftDataItems.Add("Previous Fixtures");
        mLeftDataItems.Add("Upcoming Fixtures");
        mLeftDataItems.Add("Previous Competitions");
        mLeftDataItems.Add("Upcoming Competitions");
        mLeftDataItems.Add("Settings");
        mLeftDataItems.Add("Social");

        mLeftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mLeftDataItems);
        mLeftDrawer.Adapter = mLeftAdapter;
        mLeftDrawer.ItemClick += MenuListView_ItemClick;

        mDrawerToggle = new MyActionBarDrawerToggle(this, mDrawerLayout, Resource.String.openDrawer, Resource.String.closeDrawer);

        if (bundle != null)
        {
            if (bundle.GetString("DrawerState") == "Opened")
            {
                SupportActionBar.SetTitle(Resource.String.openDrawer);
            }
            else
            {
                SupportActionBar.SetTitle(Resource.String.closeDrawer);
            }
        }
        else
        {
            SupportActionBar.SetTitle(Resource.String.closeDrawer);
        }

        Android.Support.V4.App.FragmentTransaction trans = SupportFragmentManager.BeginTransaction();

        trans.Add(Resource.Id.fragmentContainer, mHomeFragment);
        trans.Add(Resource.Id.fragmentContainer, mLogInFragment);
        trans.Hide(mLogInFragment);
        trans.Add(Resource.Id.fragmentContainer, mMatchCentreFragment);
        trans.Hide(mMatchCentreFragment);
        trans.Add(Resource.Id.fragmentContainer, mPrevFixFragment);
        trans.Hide(mPrevFixFragment);
        trans.Add(Resource.Id.fragmentContainer, mUpcomFixFragment);
        trans.Hide(mUpcomFixFragment);
        trans.Add(Resource.Id.fragmentContainer, mPrevCompFragment);
        trans.Hide(mPrevCompFragment);
        trans.Add(Resource.Id.fragmentContainer, mUpcomCompFragment);
        trans.Hide(mUpcomCompFragment);
        trans.Add(Resource.Id.fragmentContainer, mSettingsFragment);
        trans.Hide(mSettingsFragment);
        trans.Add(Resource.Id.fragmentContainer, mSocialFragment);
        trans.Hide(mSocialFragment);



        mCurrentFragment = mHomeFragment;
        trans.Commit();

    }


    void MenuListView_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
    {
        Android.Support.V4.App.Fragment fragment = null;

        switch (e.Id)
        {
            case 0:
                ShowFragment(mHomeFragment);
                break;
            case 1:
                ShowFragment(mLogInFragment);
                break;
            case 2:
                ShowFragment(mMatchCentreFragment);
                break;
            case 3:
                ShowFragment(mPrevFixFragment);
                break;
            case 4:
                ShowFragment(mUpcomFixFragment);
                break;
            case 5:
                ShowFragment(mPrevCompFragment);
                break;
            case 6:
                ShowFragment(mUpcomCompFragment);
                break;
            case 7:
                ShowFragment(mSettingsFragment);
                break;
            case 8:
                ShowFragment(mSocialFragment);
                break;
        }

        mDrawerLayout.CloseDrawers();
        mDrawerToggle.SyncState();
    }

    private void ShowFragment (SupportFragment fragment)
    {
        if (fragment.IsVisible)
        {
            return;
        }
        var trans = SupportFragmentManager.BeginTransaction();

        fragment.View.BringToFront();
        mCurrentFragment.View.BringToFront();

        trans.Hide(mCurrentFragment);
        trans.Show(fragment);

        trans.AddToBackStack(null);
        mStackFragments.Push(mCurrentFragment);
        trans.Commit();

        mCurrentFragment = fragment;


    }
    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {

            case Android.Resource.Id.Home:
                //The hamburger icon was clicked which means the drawer toggle will handle the event


                mDrawerToggle.OnOptionsItemSelected(item);
                return true;

            case Resource.Id.action_refresh:
                //Refresh
                return true;

            case Resource.Id.action_help:


                return true;

            default:
                return base.OnOptionsItemSelected(item);
        }
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.drawer_menu, menu);
        return base.OnCreateOptionsMenu(menu);
    }

    protected override void OnSaveInstanceState(Bundle outState)
    {
        if(mDrawerLayout.IsDrawerOpen((int)GravityFlags.Left))
        {
            outState.PutString("DrawerState", "Opened");
        }
        else
        {
            outState.PutString("DrawerState", "Closed");
        }

        base.OnSaveInstanceState(outState);
    }

    protected override void OnPostCreate(Bundle savedInstanceState)
    {
        base.OnPostCreate(savedInstanceState);
        mDrawerToggle.SyncState();
    }

    public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
    {
        base.OnConfigurationChanged(newConfig);
        mDrawerToggle.OnConfigurationChanged(newConfig);
    }

}

}

这是我的一个片段(有几个但到目前为止它们都包含基本相同的代码)

public class PrevFixFragment : Android.Support.V4.App.Fragment
{
    Button button;


    public PrevFixFragment()
    {

    }
    public static Android.Support.V4.App.Fragment newInstance(Context context)
    {
        PrevFixFragment fragment = new PrevFixFragment();
        return fragment;
    }
    public override void OnCreate(Bundle bundle)
    {


        base.OnCreate(bundle);

    }
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.PreviousFixtures, null);

        button = View.FindViewById<Button>(Resource.Id.upcombutton);
        button.Click += StartNewActivity;

        return view;

    }

    void StartNewActivity(object sender, EventArgs e)
    {
        Intent intent = new Intent(Activity, typeof(UpcomFixActivity));
        StartActivity(intent);
    }


}

}

2 个答案:

答案 0 :(得分:0)

尝试将以下行移至覆盖void OnViewCreated 覆盖void onActivityCreated

    button = View.FindViewById<Button>(Resource.Id.upcombutton);
    button.Click += StartNewActivity;

答案 1 :(得分:0)

您必须在下面的语句中使用小写View编写view

button = View.FindViewById<Button>(Resource.Id.upcombutton);