views:

59

answers:

3

Hello,

I am currently trying the activity testing tutorial (Found here), and have a problem. It seems that whenever I try to call something inside the UIThread, I get a java.lang.NullPointerException.

public void testSpinnerUI() {
    mActivity.runOnUiThread( new Runnable() {  
        public void run() {  
            mSpinner.requestFocus();
        }  
    });  
}

This gives me:

Incomplete: java.lang.NullPointerException

and nothing else. I have tried this on two different samples now, with the same result. I tried with a try/catch clause around the mSpinner.requestFocus() call, and it seems that mSpinner is null inside the thread. I have set it properly up with the setUp() function found in the same sample, and a quick assertNotNull( mSpinner ) shows me that mSpinner is in fact not null after the setUp() function. What can be the cause of this?

EDIT; ok, some more testing has been done. It seems that the application that is being tested resets between each test. This essentially makes me have to reinstantiate all variables between each test. Is this normal?

A: 

I should perhaps have specified explicitly that this is concerning Android in connection with JUnit.

Bendik Solheim
+1  A: 

Ok, so it seems I got it working now. At least to a certain point. I am still not able to run anything inside runOnUiThread(), but I found that using the @UiThreadTest annotation gave me satisfactory results. I no longer get NullPointerExceptions, and the setUp() method even runs correctly. I have no idea what was causing this, but hey, at least it is working :)

Bendik