Forum Xamarin.Android

When I run my Application that time i getting a error or Exception Dialog box.

Help me``

MY mainActivity Code :

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using SupportToolbar = Android.Support.V7.Widget.Toolbar;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using System.Collections.Generic;

namespace final_demo
{
[Activity(Label = "final_demo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : ActionBarActivity
{
private SupportToolbar mToolbar;
private MyActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mLeftDrawer;
private ArrayAdapter mLeftAdapter;

    private List<string> mLeftDataSet;
    private HomeFragment homeFragment;
    private ProfileFragment profileFragment;
    private SupportFragment mCurrentFragment = new SupportFragment();
    private Stack<SupportFragment> mStackFragments;





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

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
        mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer);
        homeFragment = new HomeFragment();

        profileFragment = new ProfileFragment();
        mStackFragments = new Stack<SupportFragment>();


        mLeftDrawer.Tag = 0;


        SetSupportActionBar(mToolbar);

        mLeftDataSet = new List<string>();
        mLeftDataSet.Add("Left Item 1");
        mLeftDataSet.Add("Left Item 2");
        mLeftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mLeftDataSet);
        mLeftDrawer.Adapter = mLeftAdapter;


        mLeftDrawer.ItemClick += mLeftDrawer_ItemClick;



        mDrawerToggle = new MyActionBarDrawerToggle(
       this, //Host Activity
       mDrawerLayout, //DrawerLayout
       Resource.String.OpenDrawer, //Opened Message
       Resource.String.CloseDrawer //Closed Message
       );

        mDrawerLayout.SetDrawerListener(mDrawerToggle);
        //SupportActionBar.SetHomeButtonEnabled(true);
        //SupportActionBar.SetDisplayShowTitleEnabled(true);
        mDrawerToggle.SyncState();

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

            else
            {
                //SupportActionBar.SetTitle(Resource.String.closeDrawer);
            }
        }

        else
        {
            //This is the first the time the activity is ran
            //SupportActionBar.SetTitle(Resource.String.closeDrawer);
        }

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

        tx.Add(Resource.Id.main, homeFragment);
        tx.Add(Resource.Id.main, profileFragment);
        tx.Hide(profileFragment);

        mCurrentFragment = homeFragment;
        tx.Commit();

    }

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

        switch (e.Id)
        {
            case 0:
                ShowFragment(homeFragment);
                break;
            case 1:
                ShowFragment(profileFragment);
                break;



        }

        //SupportFragmentManager.BeginTransaction().Replace(Resource.Id.main, fragment).Commit();


        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;

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

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.MyMenu, 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);
    }

}

}

Best Answer

Answers

  • Thank you @LoriLalonde

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    You're welcome @RohitRamnani! I'm always happy to help!

Sign In or Register to comment.