Application supports minSdk=14
and have ActionBar
on every Activity
.
Application works fine since a long time and shows ActionBar
on every Activity
. Now I am writing Android JUnit
Testcases
but it gives NullPointException
on line
getActionBar().setDisplayHomeAsUpEnabled(true);
The bottom line is getActionBar()
works on devices but returns null
during test cases. If I remove the line then testcases are fine.
Here's how activity looks like.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_order); getActionBar().setDisplayHomeAsUpEnabled(true);
// other stuff under it
}
JUnits
are simple Android JUnit Testcases without any framework.
public class OrderActivityTest extends ActivityUnitTestCase<OrderActivity> { private OrderActivity activity; public OrderActivityTest() { super(OrderActivity.class); } protected void setUp() throws Exception { super.setUp(); Intent intent = new Intent(getInstrumentation().getTargetContext(),OrderActivity.class); startActivity(intent, null, null); activity = getActivity(); } public void testPreConditions() { assertTrue(activity != null); } }
And finally here's the exception while running test case.
java.lang.NullPointerException
at com.example.name.OrderActivity.onCreate(OrderActivity.java:176)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at com.example.name.test.OrderActivityTest.setUp(OrderActivityTest.java:23)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Line 176 is getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar() is probably returning null here. The title needs to be visible, make sure you don't have things like this there:
This would cause an error in getActionBar() being null.