Toggling sliding menu from fragments

Hi

I have a view pager in my app which is having many fragments. The fragment is having a button. I am having a slidingfragment activity. I want to open the sliding menu when i click the button in the fragment. Here is my code of Main activity:

public class MainActivity : SlidingFragmentActivity
{


    protected static  IList<Fragment> fragments;

    protected override void OnCreate (Bundle bundle)

    {
        base.OnCreate (bundle);


        var vp = new ViewPager(this)
        {
            Id = "VP".GetHashCode()
        };
        SetContentView(vp);

        fragments = new List<Fragment>();
        // Set our view from the "main" layout resource
        //SetContentView (Resource.Layout.Main);
        ColorPagerAdapter c = new ColorPagerAdapter(SupportFragmentManager);

        vp.PageSelected += (sender, args) =>
        {


            Console.WriteLine( args.P0);

            var viewtemp = vp.Adapter.GetItemPosition(args.Position);



            switch (args.P0)
            {
                case 0:
                SlidingMenu.TouchModeAbove = TouchMode.Margin;
                break;
                default:
                SlidingMenu.TouchModeAbove = TouchMode.Margin;
                break;
            }
        };

        vp.Adapter = c;



        vp.SetCurrentItem(0, true);
        SlidingMenu.TouchModeAbove = TouchMode.Margin;



        SetBehindContentView(Resource.Layout.slideact);

        SlidingMenu.ShadowWidthRes = Resource.Dimension.shadow_width;
        SlidingMenu.BehindOffsetRes = Resource.Dimension.slidingmenu_offset;
        SlidingMenu.ShadowDrawableRes = Resource.Drawable.shadow;
        SlidingMenu.FadeDegree = 0.25f;
        SlidingMenu.Mode = MenuMode.Right;
        SlidingMenu.TouchModeAbove = TouchMode.Margin;
        Button b = SlidingMenu.FindViewById<Button> (Resource.Id.button1);
        b.Click += delegate {
            Console.WriteLine(b + " Clicked");
            c.addfrag();
            c.NotifyDataSetChanged();


    };


    public void toggler()
    {


        SlidingMenu.Toggle ();
    }
    public class ColorPagerAdapter : FragmentPagerAdapter
    {
        private readonly IList<Fragment> _fragments;

        private readonly int[] _colors = new[]
        {
            Resource.Color.red,

        };

        public ColorPagerAdapter(FragmentManager fm) 
            : base(fm)
        {

            var c= Resource.Color.red;
                FragmentClass frag = new FragmentClass(c);

                fragments.Add(frag);

        }

        public void addfrag()
        {
            foreach (var c in _colors)

                fragments.Add(new FragmentClass(c));
        }

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

        public override Fragment GetItem(int p0)
        {
            return fragments[p0];
        }
    }
}

And here is the code for the fragment class:

    class FragmentClass:Android.Support.V4.App.Fragment
{
    private int _colorRes = -1;

    public FragmentClass() 
        : this(Resource.Color.white)
    { }


    public FragmentClass(int colorRes)
    {
        _colorRes = colorRes;
        RetainInstance = true;
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {


        if (null != savedInstanceState)
            _colorRes = savedInstanceState.GetInt("_colorRes");
        var color = Resources.GetColor(_colorRes);
        var v = inflater.Inflate (Resource.Layout.Main, container, false);
        var v1 = v.FindViewById<LinearLayout> (Resource.Id.myLayout);
        v1.SetBackgroundColor(color);
        var button = v.FindViewById<Button> (Resource.Id.myButton);
        MainActivity m = new MainActivity ();
        button.Click += delegate {
            m.toggler();
        };

        return v;
    }
    public override void OnSaveInstanceState(Bundle outState)
    {
        base.OnSaveInstanceState(outState);
        outState.PutInt("_colorRes", _colorRes);
    }


}

What i have done here is, I have created a function in mainactivity, called toggler, in which I am calling SlidingMenu.toggle();
Toggler is called from the button.click method in fragment class. But the sliding menu is not opening. It is showing the error:

System.NullReferenceException: Object reference not set to an instance of an object
at SlidingMenuSharp.App.SlidingFragmentActivity.get_SlidingMenu () [0x00007] in /Users/admin/Desktop/SlidingMenuSharp-master 2/SlidingMenuSharp/App/SlidingFragmentActivity.cs:71
at tym.MainActivity.toggler () [0x00002] in /Users/admin/Projects/new/tym/MainActivity.cs:98
at tym.FragmentClass/c__AnonStorey1.<>m__4 (object,System.EventArgs) [0x00007] in /Users/admin/Projects/new/tym/FragmentClass.cs:42
at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/b76e8ec4/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Views.View.cs:1573
at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/b76e8ec4/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Views.View.cs:1543
at at (wrapper dynamic-method) object.77b8b058-c885-452d-9852-5e22eda8de9e (intptr,intptr,intptr)
at

Please have a look at this and help me.

Thanks.
Vyshakh

Posts

  • CheesebaronCheesebaron DKInsider, University mod

    You are doing it wrong, that is for sure.

    MainActivity m = new MainActivity ();
    button.Click += delegate {
        m.toggler();
    };
    

    This will not work, as you are instantiating a NEW MainActivity rather using the one Android already instantiated for you.

    In the Fragment class there is a Activity property which you might want to use instead. However it is bad practice to call methods directly on the Activity as you can't always know if it is alive or showing in foreground.

    Instead expose some events in your Fragment that the Activity listens to.

  • VyshakhK.NVyshakhK.N USMember

    Thanks a lot. It finally worked. But i will try to expose the events in my fragment.

  • JensRatseyJensRatsey US
    edited October 2013

    Is there support for Android 2.3.3 when inheriting from SlidingFragmentActivity? When I try it, the app crashes with:

    java.lang.NoClassDefFoundError: com.actionbarsherlock.R$styleable

    This can be replicated by simply changing the inheritance on the SlidingMenuExample from SlidingActivity to SlidingFragmentActivity.

  • CheesebaronCheesebaron DKInsider, University mod

    Create a new question as it is not related at all to "Toggling sliding menu from fragment".

Sign In or Register to comment.