我正在使用 robolectric 测试我的 Android 应用程序代码。在其中一项 Activity 中, Activity 的 onCreateOptionsMenu
中存在布局膨胀。
我正在尝试使用以下方法测试该代码:
activityController.create().resume().visible()
以下是 onCreateOptionsMenu 的内容:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
LayoutInflater inflater = LayoutInflater.from(this);
View v = inflater.inflate(R.layout.custom_action_bar, null);
return super.onCreateOptionsMenu(menu);
}
但执行卡在 inflater.inflate(R.layout.custom_action_bar, null)
行上。奇怪的是,同样的代码在粘贴到 onCreate
方法中时可以完美地工作。
谁能帮我解决这里可能出现的问题吗?我是 Robolectric 和 android 的新手。
最佳答案
onCreateOptionsMenu()
旨在从 MenuInflater
创建菜单。这里使用 LayoutInflater.from(this) 是一种代码味道。
参见https://developer.android.com/guide/topics/ui/menus#options-menu
关于java - Robolectric::LayoutInflator.inflate() 卡在 onCreateOptionsMenu 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941519/