所以我有一个通过发出HTTP请求开始的活动。我想在请求期间显示进度对话框,这没有问题。但是添加对话框会破坏我的测试,现在我收到以下错误:
java.lang.NullPointerException
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1246)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:664)
at org.robolectric.shadows.ShadowHandler.routeMessage(ShadowHandler.java:125)
at org.robolectric.shadows.ShadowHandler.access$100(ShadowHandler.java:25)
at org.robolectric.shadows.ShadowHandler$1.run(ShadowHandler.java:110)
at org.robolectric.util.Scheduler$PostedRunnable.run(Scheduler.java:162)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:107)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:92)
at org.robolectric.util.Scheduler.advanceToLastPostedRunnable(Scheduler.java:68)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:25)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:228)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:267)
at org.robolectric.util.ActivityController.create(ActivityController.java:144)
at org.robolectric.util.ActivityController.create(ActivityController.java:154)
at com.showmobile.android.showmobile.settings.BasicInfoActivityTest.setup(BasicInfoActivityTest.java:90)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
以下是测试中的一些删节代码:
private final ActivityController<BasicInfoActivity> controller = buildActivity(BasicInfoActivity.class);
private BasicInfoActivity activity;
@Before
public void setup() {
activity = controller.create().start().resume().visible().get();
}
该activity = controller.create()行是错误发生的地方。 以下是活动本身,减少:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_info_activity);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowTitleEnabled(true);
/** Some other irrelevant code goes here **/
fetchCurrentUserProfile();
}
private void fetchCurrentUserProfile() {
progressDialog = ProgressDialogBuilder.buildProgressDialog(this);
progressDialog.show();
userService.loadCurrentUser().getAsynchronously(getUserProfileOperation());
}
private DeferredOperation<GetUserPrivateProfileApiResponse> getUserProfileOperation() {
return new DeferredOperation<GetUserPrivateProfileApiResponse>() {
@Override
public void success(GetUserPrivateProfileApiResponse response) {
/** Success handling code goes here **/
}
@Override
public void complete(GetUserPrivateProfileApiResponse response) {
if (profile == null) {
profile = userService.getCurrentUser();
}
progressDialog.dismiss();
}
};
}
如果我删除progressDialog.show();,则错误消失。我试图找到Robolectric测试的例子,其中活动从加载对话开始,但到目前为止没有运气。从单步执行堆栈跟踪看起来可能存在某种问题,Dialog没有实际的Context可以使用,但我不确定。在此先感谢您的帮助!
编辑:似乎在onResume()而不是onCreate()中显示progressDialog修复了NPE,但在实际的Android环境中它并不重要。
答案 0 :(得分:0)
在我自己的情况下,ProgressDialog也是问题,一旦我找到了一种方法来避免调用.show()方法,我的测试也会通过。