Activity does not have an options menu

删除回忆录丶 提交于 2019-12-07 15:22:37

问题


I was trying to simulate click on Menu Item present in Toolbar using Robolectric, using the following code

ShadowActivity shadowActivity = Shadows.shadowOf(activity);
shadowActivity.clickMenuItem(R.id.action_logout);

But it is giving me an error:

java.lang.RuntimeException: Activity does not have an options menu! Did you forget to call super.onCreateOptionsMenu(menu)

Code of the Activity:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);

    ButterKnife.bind(this);

    setToolbarIcons();
    toolbar.inflateMenu(R.menu.menu_splash);
    toolbar.setOnMenuItemClickListener(this);
}


@Override
public boolean onMenuItemClick(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_logout){
        //Doing Some task here...
        return true;
    }
    return false;
}

Could you please help me with the Error?


回答1:


The problem seems to be, that you do not set your toolbar as the supportActionbar/ActionBar.

I have never seen such a toolbar-inflation but normally it is done like this:

public class MyActivity extends AppCompatActivity {

    @Bind(R.id.toolbar) Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);            
        butterKnife.bind(this);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.actions_myactivity, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //handle clicked menuItem

        return super.onOptionsItemSelected(item);
    }
}

As your error is :

Activity does not have an options menu!

Also the Testmethod should look like this

@Test
public void clickOnMenuItem(){
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    Menu menu =  mToolbar.getMenu();
    shadowActivity.onCreateOptionsMenu(menu);
    shadowActivity.clickMenuItem(R.id.action_logout);
}

I think this is the way to go!




回答2:


After doing the changes in my Activity class and digging in Robolectric and ToolBar I made some changes in the test class too, which is working fine

@Test
public void clickOnMenuItem(){
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    Menu menu =  mToolbar.getMenu();
    shadowActivity.onCreateOptionsMenu(menu);
    shadowActivity.clickMenuItem(R.id.action_logout);
}


来源:https://stackoverflow.com/questions/34869746/activity-does-not-have-an-options-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!