1

I am developing an Android app with several Activity classes. I am using Robolectric for unit testing. In the test setup I call onCreate(null) in the Activity being tested:

@Before
public void setUp() throws Exception {
    activity = new StartARound();
    activity.onCreate(null);
    btnSelectCourse = (Button) activity.findViewById(com.catalyst.golfapp.R.id.btnSelectCourse);
    btnAddACourse = (Button) activity.findViewById(com.catalyst.golfapp.R.id.btnAddACourse);
    btnShowLocation = (Button) activity.findViewById(com.catalyst.golfapp.R.id.btnShowLocation);
    textView1 = (TextView) activity.findViewById(com.catalyst.golfapp.R.id.textView1);
}

The problem is, the onCreate method for the Activity I'm testing calls:

getActionBar().setDisplayHomeAsUpEnabled(true);

Execution of this line of code produces a java.lang.NullPointerException error and the tests do not run. Any ideas, or additional code you'd like to examine?

4

2 回答 2

0

getActionBar()未在最新版本的 Robolectric 中实现。

这是 ShadowActivity 的源文件: https ://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowActivity.java

此 API 可用于查看您何时使用 robolectric 中不存在的功能:http://pivotal.github.com/robolectric/javadoc/com/xtremelabs/robolectric/Robolectric.html#logMissingInvokedShadowMethods ()

于 2013-02-17T01:50:48.910 回答
0

顺便说一句,getActionBar()不推荐使用。而是使用包含较新操作栏的 Android-AppCompat-v7。你可以使用它getSupportActionBar()

于 2015-05-08T02:10:06.727 回答