本文介绍了Robolectric:actionBar.hide()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是新来的Android单元测试和我使用Robolectric作为一个测试框架。我用Robolectric 2.2。I'm new to Android unit testing and I'm using Robolectric as a testing framework. I use Robolectric 2.2.我想测试一个活动是这样的:I'm trying to test an activity which is like that : public class LoginActivity extends SherlockActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);ActionBar abs = getSupportActionBar();abs.hide();}这是我的测试类:and here's my test class : @RunWith(RobolectricTestRunner.class)public class LoginActivityTest {@Before public void setUp() throws Exception{ System.setProperty("dexmaker.dexcache", "/sdcard"); activity = Robolectric.buildActivity(LoginActivity.class).create().get();}@Testpublic void should_loginActivity_created() throws Exception { assertNotNull(activity);}我得到这个错误:I'm getting this error : java.lang.NullPointerException at auth.LoginActivity.onCreate(LoginActivity.java:119)该行指, abs.hide(); This line refers to abs.hide();注:我试过西安的主旨并没有奏效。NOTE : I tried Xian's Gist and it did not work.我也尝试创建ShadowSherlockActivity如这但我不知道如何使用这个阴影类创建类似的活动:Also I try to create ShadowSherlockActivity like this But I have no idea how to use this shadow class to create activity like :activity = Robolectric.buildActivity(LoginActivity.class).create().get();注2:我尝试使用Robolectric快照2.3,但它并没有解决。NOTE 2 : I try to use Robolectric Snapshot 2.3 but it did not solved.感谢。推荐答案随着@ersentekin和我计算出的评论,这里是一个的要点与建议相对=nofollow>要点 marsucsb ,并修改它与Robolectric 2.2工作+ As @ersentekin and I worked out in the comments, here is a Gist that takes @Xian's Gist with a suggestion from marsucsb and modifies it to work with Robolectric 2.2+ 这篇关于Robolectric:actionBar.hide()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 11:18