mercredi 4 février 2015

Unable to get activity in Unit testing

I am trying to create some tests for my app but for some reasons I am unable to get the activity I want and the test fails. Here is my code:



public class HomeFragmentTest extends ActivityInstrumentationTestCase2<Home>
{
private Home home;
FragmentManager fragmentManager;
RadioGroup radioGroup;

public HomeFragmentTest() {
super(Home.class);
}

@Override
protected void setUp() throws Exception
{
super.setUp();
setActivityInitialTouchMode(false);

try
{

this.home = getActivity();
RadioButton buttonZone = (RadioButton)home.findViewById(R.id.tabZoneHome);
fragmentManager= home.getFragmentManager();
//fragmentManager.executePendingTransactions();
}catch (Exception e)
{
e.printStackTrace();
}
}


I don't understand why getActivity() method doesn't work and I keep getting this error:



Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''


What am I doing wrong? Also is this the right way to get the activity and how to call findviewbyid()?


Aucun commentaire:

Enregistrer un commentaire