这个问题在这里已有答案:
我正在使用roboelectric和面向空指针异常编写函数的单元测试,但我已经分配了变量。我对此很新,所以如果有任何错误,请原谅我。
@Before
public void setup() {
intent = new Intent(context, Xyz.class);
intent.putExtra("abc", "abc");
xyz = Robolectric.buildActivity(Xyz.class, intent)
.create().start().get();
}
@Test
public void onConfigurationChanged() {
int currentOrientation = xyz.getRequestedOrientation();
RelativeLayouot gifframe = xyz.findViewById(R.id.gif_frame);
assertNotNull(gifframe);
assertEquals(gifframe.getVisibility(), VISIBLE);
}
}
我希望你的gifframe能够获得可见性,但gifframe返回一个空值。在我搜索解决方案的任何地方都是在调用之前分配值。我在上一行中分配了值。
当你这样做
RelativeLayouot gifframe = xyz.findViewById(R.id.gif_frame);
确保你做了setContentView(R.layout.thefilewhichhas_gif_frame)
意思是确保它知道它需要去哪里寻找gif_frame
。
看起来findViewById
正在返回null
。基本上没有view
可用给定的Id
。这是调试的正确位置。
我建议您附加一个调试器,看看加载的view
是什么,并检查它是否具有所需的视图。
希望这可以帮助。