androidandroid-layoutoptions-menu

Unable to use LayoutInflater from within onOptionsItemSelected


I am trying to inflate a layout through an options menu item. code i tried to use is as follows

public class Test01 extends Activity
{
    @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
        ...
        setContentView(R.Layout.main);
        ...
    }
    @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            theMenu=menu;
            MenuInflater inflater=getMenuInflater();
            inflater.inflate(R.menu.main_optmenu, theMenu);
            return(super.onCreateOptionsMenu(menu)); 
        }
    public boolean onOptionsItemSelected(MenuItem item)
        {
            switch(item.getItemId())
            {
                case R.id.item1:
                LayoutInflater li=(LayoutInflater)getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                li.inflate(R.layout.mylayout,this,true);
            case R.id.item2:
                ...
            default:
                ....        
        }
    }
}

i get 2 errors - "getContext() is undefined" so after searching i changed it to getApplicationContext() then i am able to obtain LAYOUT_INFLATER_SERVICE.

But then in the second line the ViewGroup parameter is not accepted.. Error message is "The method inflate(int, ViewGroup, boolean) in the type LayoutInflater is not applicable for the arguments (int, test01, boolean)"

It seems i am not providing the correct values for the parameters, The menu itself works fine as i am able to replace LayoutInflater with setContentView

I would really appreciate your help , "thanks


Solution

  • Replace

    li.inflate(R.layout.mylayout,this,true);

    with

    li.inflate(R.layout.mylayout,null);