这与在Android上使用Robolectric框架进行单元测试有关。我在运行正常时没有问题的代码上得到空指针异常。我刚刚开始使用摩托车,因此它可能非常简单。Android/Robolectric框架工作 - 实例化活动在getResource上返回null
下面是测试调用代码:
@Test
public void testInitUtilsInitSequenceNumberIsRandom() {
// create an activity for reference
InitUtils initUtils = new InitUtils();
// do static initialization to parse questions into memory
InitUtils.initialize(initUtils); // <============ the call from roboelectric framework
// retreive app state
AppState appState = (AppState) initUtils.getApplicationContext();
// fill in later
fail("not implemented");
}
以下是在InitUtils内调用的方法,其崩溃
/** * 加载XML到{@see mQuestions}类成员可变 * */
public static void initializeQuestions(Activity activity, AppState appState) {
/* create XML Parser */
XmlResourceParser questionBatch;
/* local question variable */
Question question = null;
/* retrieve the XML for parsing */
// =============== This returns null ==============================
questionBatch = activity.getResources().getXml(R.xml.questions);
/* Parse the XML */
int eventType = -1;
/* iterate through XML */
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
/* Get the questions */
// ================================= NPE exception ======================
String strName = questionBatch.getName();
...etc
是不是有什么特别的,我需要为此做检索资源?
是它崩溃哪行? – jlindenbaum 2011-06-20 20:14:09
@Jack BenNimble你LogCat说在这里显示? – Herry 2011-10-08 13:15:24