Android / Robolectric frame work - Instantiated activity returns null on getResource

94 views
Skip to first unread message

jackbenimble

unread,
Mar 30, 2011, 9:08:33 PM3/30/11
to Robolectric
This has to do with using the Robolectric framework for unit testing
on android. I'm getting a null pointer exception on code which has no
problem when running normally. I'm just starting on the roboelectric,
so it's probably pretty simple.

I noticed another member had the same problem, but apparently it
hasn't been resolved, so I'm posting just in case I missed something.

Here is the calling code:

@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");

}

Here is the method called within in InitUtils which crashes

/** * Loads the XML into the {@see mQuestions} class member variable *
*/ 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
Is there something special I need to do for this to retrieve the
resource?

http://stackoverflow.com/questions/5340239/robolectriceclipse-cant-find-resources

Jack



I also posted on SO -

Giorgio Vespucci

unread,
Apr 7, 2011, 5:44:15 AM4/7/11
to robol...@googlegroups.com
Hi Jack
Same problem for me at https://groups.google.com/forum/#!topic/robolectric/njVMqhQXZY4

Really nobody knows?

Thank you
Reply all
Reply to author
Forward
0 new messages