java - Robolectric::LayoutInflator.inflate() 卡在 onCreateOptionsMenu 中

标签 java android robolectric

我正在使用 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/

相关文章:

java - 对 hibernate-generic-dao 等的评论和经验?

java - 位运算符 java

android - 如何确定 Android Activity 正在使用哪个显示器?

java - 从 Robolectric 下的服务获取 WindowManager 时出现 NullPointerException

robolectric - Robolectric 测试中的卡住时间

java - 使用 ETag 但状态代码始终为 200 而不是 304

java - XML 签名引用摘要使用父命名空间

android - 如何停止后台线程, `interupt` 不工作

android - 链接 Sceneform 节点/对象以一起缩放和移动

Android:Robolectric 不支持 API 级别 1