我试图使用以下代码在工具栏中模拟点击菜单项,并使用Robolectric来进行测试:
ShadowActivity shadowActivity = Shadows.shadowOf(activity);
shadowActivity.clickMenuItem(R.id.action_logout);
但是它给我报错了:
java.lang.RuntimeException: Activity does not have an options menu! Did you forget to call super.onCreateOptionsMenu(menu)
活动代码:
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;
}
能请你帮我解决这个错误吗?
onCreate
方法吗?工具栏在哪里初始化? - JDurstberger