我有一个使用片段并实现了 ActionBarCompat 的 android 应用程序。我编写了如下所示的简单 Robolectric 测试:
@Test
public void shouldNotBeNull() throws Exception {
MainFragment mainFragment = new MainFragment();
startFragment(mainFragment);
assertThat(mainFragment, notNullValue());
assertThat(mainFragment.getActivity(), notNullValue());
}
private void startFragment(MainFragment fragment) {
FragmentActivity activity = new FragmentActivity();
shadowOf(activity).callOnCreate(null);
shadowOf(activity).callOnStart();
shadowOf(activity).callOnResume();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(fragment, null);
fragmentTransaction.commit();
}
我收到以下异常:
java.lang.NullPointerException: null
at android.app.Activity.invalidateOptionsMenu(Activity.java:2595)
at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:30)
at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:572)
at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:708)
at com.example.android.ui.MainFragment.onCreate(MainFragment.java:599)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37)
at org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:198)
at org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56)
at org.robolectric.shadows.ShadowHandler.post(ShadowHandler.java:51)
at android.os.Handler.post(Handler.java)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1322)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
at com.example.android.ui.MainFragmentTest.startFragment(MainFragmentTest.java:36)
at com.example.android.ui.MainFragmentTest.shouldNotBeNull(MainFragmentTest.java:22)
当使用这样的东西时:
MainActivity activity = new MainActivity();
shadowOf(activity).callOnCreate(null);
shadowOf(activity).callOnStart();
shadowOf(activity).callOnResume();
其中 MainActivity 是 MainFragment 的持有者活动,我得到以下异常:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.res.builder.RobolectricPackageManager.getActivityInfo(RobolectricPackageManager.java:59)
at android.support.v7.app.ActionBarActivityDelegate.getUiOptionsFromMetadata(ActionBarActivityDelegate.java:157)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:53)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at com.example.android.ui.MainActivity.onCreate(MainActivity.java:45)
at org.robolectric.shadows.ShadowActivity.invokeReflectively(ShadowActivity.java:176)
at org.robolectric.shadows.ShadowActivity.callOnCreate(ShadowActivity.java:121)
at com.example.android.ui.MainFragmentTest.startFragment(MainFragmentTest.java:28)
at com.example.android.ui.MainFragmentTest.shouldNotBeNull(MainFragmentTest.java:22)
我怀疑这可能与 ActionBarCompat 和 Robolectric 不兼容有关。任何帮助,将不胜感激。