Android 未找到使用资源的自定义视图上的 InstrumentationTestCase
Android InstrumentationTestCase on custom view using resource, not found
我有一个动态创建视图的自定义视图。
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
如何获取测试以查看我的颜色资源?
可以通过 getTargetContext() 实现。我以前
myCustomView = new MyCustomView(getInstrumentation().getContext());
并将其替换为
myCustomView = new MyCustomView(getInstrumentation().getTargetContext());
我有一个动态创建视图的自定义视图。
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
如何获取测试以查看我的颜色资源?
可以通过 getTargetContext() 实现。我以前
myCustomView = new MyCustomView(getInstrumentation().getContext());
并将其替换为
myCustomView = new MyCustomView(getInstrumentation().getTargetContext());