Robolectric 1.1在布局文件不在res/layout/目录中时无法加载布局文件。

3
当我在 Robolectric 1.1 中运行测试时,在膨胀布局文件时会出现以下错误:
   java.lang.RuntimeException: Could not find layout layout/home_layout
       at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:92)
       at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
       at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)

有人知道为什么会出现这个错误吗?根据我所看到的,robolectric版本1.1在加载布局文件的方式上没有改变,它会搜索layout/文件夹:

   private ViewNode getViewNodeByLayoutName(String layoutName) {
        if (layoutName.startsWith("layout/") && !qualifierSearchPath.isEmpty()) {
            String rawLayoutName = layoutName.substring("layout/".length());
            for (String location : qualifierSearchPath) {
                ViewNode foundNode = viewNodesByLayoutName.get("layout-" + location + "/" + rawLayoutName);
                if (foundNode != null) {
                    return foundNode;
                }
            }
        }
        return viewNodesByLayoutName.get(layoutName);
    }

如果布局文件在另一个目录中(例如layout-normal-hdpi),则会出现此错误..

有没有人知道解决办法?

1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
0

合格的资源

在上述场景中,在RoboElectric 2.4(也许是早期版本;我没有检查过)中,可以为资源指定限定符。请参阅合格的资源上的RoboElectric页面。

例如,如果有以下资源:

 layout/settings_config.xml
 layout-normal/settings_config.xml
 layout-normal-hdpi/settings_config.xml

如果使用属性@Config(qualifiers="normal-hdpi"),则可以确保使用layout-normal-hdpi/settings_config.xml资源。

使用不同的RobolectricTestRunner更改资源路径

另一种方法(可能不适用于此场景,但如果在build.gradle中更改了资源路径,则很好)是作为RobolectricTestRunner实现的一部分加载不同的资源。这里有一个很好的示例:

https://stackoverflow.com/a/29223625/3063884


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,