Java 当我已经赋值时,如何修复空指针异常
我正在使用roboelectric为函数编写单元测试,并面临空指针异常,但我已经分配了变量。 我对这个很陌生,所以如果有任何错误,请原谅我Java 当我已经赋值时,如何修复空指针异常,java,android,unit-testing,robolectric,Java,Android,Unit Testing,Robolectric,我正在使用roboelectric为函数编写单元测试,并面临空指针异常,但我已经分配了变量。 我对这个很陌生,所以如果有任何错误,请原谅我 @Before public void setup() { intent = new Intent(context, Xyz.class); intent.putExtra("abc", "abc"); xyz = Robolectric.buildActivity(Xyz.class, intent) .
@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返回的是空值。无论我在哪里寻找解决方案,都是在调用之前赋值。我已在前一行中赋值。看起来
findViewById
正在返回null
。对于给定的Id
,基本上没有可用的视图。这是调试的正确位置
我建议您附加一个调试器,查看正在加载的视图是什么,并检查它是否具有所需的视图
希望这有帮助 看起来findViewById
正在返回null
。对于给定的Id
,基本上没有可用的视图。这是调试的正确位置
我建议您附加一个调试器,查看正在加载的视图是什么,并检查它是否具有所需的视图
希望这有帮助 当你这样做的时候
RelativeLayouot gifframe = xyz.findViewById(R.id.gif_frame);
确保您设置了内容视图(R.layout.thefilewhichhas\u gif\u frame)
意思是确保它知道需要去哪里寻找gif\u帧
当你这样做的时候
RelativeLayouot gifframe = xyz.findViewById(R.id.gif_frame);
确保您设置了内容视图(R.layout.thefilewhichhas\u gif\u frame)
意思是确保它知道需要去哪里寻找gif\u帧
它只是意味着xyz.findViewById(R.id.gif_frame)
返回null,因此xyz
中没有视图
具有R.id.gif\u帧的id
。因此,请确保检查R.id.gif_frame
的值,并检查xyz
中是否有视图具有该id
。如果您已分配了某些内容,请为此使用调试器。很明显,有些东西是null
。是的,我给R.id.gif\u frame赋值的值是null,但我不明白为什么它是null。它只是意味着xyz.findViewById(R.id.gif\u frame)
返回null,因此xyz
中没有视图
具有R.id.gif\u帧的id
。因此,请确保检查R.id.gif_frame
的值,并检查xyz
中是否有视图具有该id
。如果您已分配了某些内容,请为此使用调试器。很明显,有些东西是null
。是的,我分配的R.id.gif_frame的值是null,但我不明白为什么它是null。我尝试了这个,但它给了android.view.InflateException:XML file/home/anirudh/proj/app/build/intermediates/merged not compiled resources/debug/layout/gifframe.XML line.-1如何显示你的XML文件我尝试了这个但它给出了android.view.InflateException:XML文件/home/anirudh/proj/app/build/intermediates/merged not compiled resources/debug/layout/giframe.XML行#-1显示您的XML文件