我有一个自动创建视图的自定义视图。
public class MyCustomView extends LinearLayout {
...
private View foo() {
View view = new View(getContext());
view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.gray));
return view;
}
}
当我测试foo()时,我得到一个资源未找到异常。
public class MyCustomViewTest extends InstrumentationTestCase {
...
public void testFoo() {
View view = myCustomView.foo();
assertNotNull(view);
}
}
android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0022
如何才能通过测试来查看我的色彩资源?
答案 0 :(得分:0)
可以通过getTargetContext()实现。我以前有过
myCustomView = new MyCustomView(getInstrumentation().getContext());
并将其替换为
myCustomView = new MyCustomView(getInstrumentation().getTargetContext());