7

I try to run a test on a fragmentDialog that is anchored to a viewId in its hosting activity

  @Test
  public void largeScreenDeviceUsesPopup() {
    final FragmentUtilActivity fragmentActivity = new FragmentUtilActivity();
    fragmentActivity.onCreate(null);

    myDialogFragment = MyDialogFragment.create(FragmentUtilActivity.anchorId);
    myDialogFragment.showNow(fragmentActivity.getSupportFragmentManager(), "");

    assertThat(....)
  }

  private static class FragmentUtilActivity extends FragmentActivity  {

  public static int anchorId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      LinearLayout view = new LinearLayout(this);
      view.setId(anchorId);

      setContentView(view);
    }
  }

however i get this error:

Your activity is not yet attached to the Application instance. You can't request ViewModel before onCreate call.

how can i fix this?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

0 Answers0