如何从另一个Android类调用菜单对象

时间:2015-04-07 02:02:32

标签: java android menu drawer

我有一个带有菜单和导航抽屉(navObj类)的课程。我想从我的其他活动课程中调用菜单和抽屉。

例如,我想使用navObj类中的对象调用主活动类的菜单。

这是我的navObj类:

    public boolean onCreateOptionsMenu(Menu menu) {
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return super.onCreateOptionsMenu(menu);
   }

    public void addDrawerItems() {
    String[] drawer_Array = { "User Profile", "Setting", "Contact us", "Help", "Logout" };
    String[] desc_Array = { "Private", "Change settings", "Ask us anything", "Simple Guide", "Logs out user" };
    Integer[] image_Array = { R.drawable.user, R.drawable.setting, R.drawable.contactme, R.drawable.help, R.drawable.logout };

   //mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawer_Array);
    //mDrawerList.setAdapter(mAdapter);
    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < drawer_Array.length; i++) {
        RowItem item = new RowItem(image_Array[i], drawer_Array[i], desc_Array[i]);
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.navList);
    CustomDrawerBaseAdapter adapter = new CustomDrawerBaseAdapter(this, rowItems);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position == 0){
                Intent toProfile = new Intent(getApplicationContext(), userprofActivity.class);
                startActivity(toProfile);
                //Toast.makeText(getMsgActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();   
            }
            else if(position == 1){
                //Toast.makeText(getMsgActivity.this, "Time for an Wake!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 2){
                //Toast.makeText(getMsgActivity.this, "Time for an Eat!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 3){
                //Toast.makeText(getMsgActivity.this, "Time for an Dance!", Toast.LENGTH_SHORT).show();
            }
            else if(position == 4){
                //Toast.makeText(getMsgActivity.this, "Time for an Sleep!", Toast.LENGTH_SHORT).show();
            }   
        }
    });
  }

    public void setupDrawer() {
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        //getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            // enabling action bar app icon and behaving it as toggle button

                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getSupportActionBar().setTitle("User Menu Option");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(listView);
        menu.findItem(R.id.action_write).setVisible(!drawerOpen);
        menu.findItem(R.id.action_grabmsg).setVisible(!drawerOpen);
        menu.findItem(R.id.action_mydiary).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            System.out.println("landscape");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            System.out.println("portrait");
        }
    } 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //bundle = getIntent().getExtras();
        //String language_chosen = bundle.getString("lang_pick");

        if (id == R.id.action_write) {
            Intent writeNewMsg = new Intent(getApplicationContext(), writeMsgActivity.class);
            //writeNewMsg.putExtra("lang_pick", language_chosen);
            startActivity(writeNewMsg);
        }
        else if(id == R.id.action_grabmsg){
            Intent grabNewMsg = new Intent(getApplicationContext(), getMsgActivity.class);
            //grabNewMsg.putExtra("lang_pick", language_chosen);
            finish();
            startActivity(grabNewMsg);

        }
        else if(id == R.id.action_mydiary){

        }

        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

现在我想从另一个活动类中调用它:

    navObj nav = new navObj();
    Menu menu = null; //it wants me to make menu null inorder to call the parameters from the navObj class.
    nav.onCreateOptionsMenu(menu);

所以我的问题是如何在不传递&#34; null&#34;的参数的情况下调用它。因为它会崩溃我的应用程序。任何信息都表示赞赏。

0 个答案:

没有答案