我一直在测试我正在测试的Activity上的测试失败。我有大量的其他测试用类似的测试/实际代码做类似的事情,没有得到任何异常。这个例外总是深深埋藏在Google Android代码中,与遍历视图有关......我不知道问题是什么。代码可以在现实中找到。
我通过运行以下命令启动支持片段的活动:
Robolectric.buildActivity(PartnersActivity.class).create().start().resume().get();
with View:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="3"
android:baselineAligned="false">
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:id="@+id/main_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
/>
</LinearLayout>
和活动:
public class PartnersActivity extends ActivityBase implements
PartnersFragment.PartnerShowable, PartnersFragment.PartnerEditable,
PartnersFragment.PartnerFinishable, PartnersFragment.PartnerDeletable {
@Inject
TabletDetector tabletDetector;
@InjectView(R.id.main_container)
@Optional
FrameLayout mainContainerFrameLayout;
private boolean tablet;
private Partner updatedPartner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Injector.inject(this);
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
setContentView(R.layout.activity_fragment_container);
ButterKnife.inject(this);
tablet = tabletDetector.isTablet(mainContainerFrameLayout);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PartnersFragment.create(tablet))
.commit();
}
}
public void newPartner() {
if(tablet) {
replaceMainFragment(CreatePartnerFragment.create());
} else {
CreatePartnerActivity.start(this);
}
}
@Override
public void deletePartner() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, PartnersFragment.create(tablet))
.remove(getSupportFragmentManager().findFragmentById(R.id.main_container))
.commit();
invalidateOptionsMenu();
}
@Override
public void editPartner(Partner partner) {
replaceMainFragment(EditPartnerFragment.create(partner));
}
@Override
public void showPartner(Partner partner) {
if(tablet) {
replaceMainFragment(PartnerFragment.create(partner));
} else {
PartnerActivity.start(this, partner);
}
}
@Override
public void finishPartner(Partner partner) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, PartnersFragment.create(tablet))
.replace(R.id.main_container, PartnerFragment.create(partner))
.commit();
invalidateOptionsMenu();
}
private void replaceMainFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, fragment)
.commit();
invalidateOptionsMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Fragment fragmentById = getSupportFragmentManager().findFragmentById(R.id.main_container);
if(fragmentById == null || !(fragmentById instanceof CreatePartnerFragment))
getMenuInflater().inflate(R.menu.partners, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
} else if (id == R.id.action_create_partner) {
newPartner();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == ActivityUtils.RESULT_CODE_OK && requestCode == ActivityUtils.REQUEST_CODE_UPDATE) {
updatedPartner = (Partner) data.getSerializableExtra("partner");
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
if(updatedPartner == null) return;
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, PartnerFragment.create(updatedPartner))
.commit();
updatedPartner = null;
}
}
得到:
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.ComponentController.invokeWhilePaused(ComponentController.java:75)
at org.robolectric.util.ActivityController.start(ActivityController.java:134)
at jp.co.freee.freee.activities.PartnersActivityTest.setUp(PartnersActivityTest.java:65)
at jp.co.freee.freee.activities.PartnersActivityTest.newPartner_whenPhone_startsCreatePartnerActivity(PartnersActivityTest.java:94)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)