Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not happy with this solution, but there's a way to make it work:</p> <p>The method <code>fragment.getView()</code> is returning null because the <code>onCreateView</code> from my Fragment was never called. It should be called by the method <code>moveToState</code> from <code>android.support.v4.app.FragmentManager</code>, but didn't because <code>f.mFromLayout</code> was false.</p> <pre><code>f.mActivity = mActivity; f.mFragmentManager = mActivity.mFragments; f.mCalled = false; f.onAttach(mActivity); if (!f.mCalled) { throw new SuperNotCalledException("Fragment " + f + " did not call through to super.onAttach()"); } mActivity.onAttachFragment(f); if (!f.mRetaining) { f.mCalled = false; f.onCreate(f.mSavedFragmentState); if (!f.mCalled) { throw new SuperNotCalledException("Fragment " + f + " did not call through to super.onCreate()"); } } f.mRetaining = false; if (f.mFromLayout) { // For fragments that are part of the content view // layout, we need to instantiate the view immediately // and the inflater will take care of adding it. f.mView = f.onCreateView(f.getLayoutInflater(f.mSavedFragmentState), null, f.mSavedFragmentState); if (f.mView != null) { f.mInnerView = f.mView; f.mView = NoSaveStateFrameLayout.wrap(f.mView); if (f.mHidden) f.mView.setVisibility(View.GONE); f.onViewCreated(f.mView, f.mSavedFragmentState); } else { f.mInnerView = null; } } </code></pre> <p>So, if you add this block of code to your fragments <code>onCreate</code> method, it will work.</p> <pre><code>Field field = Fragment.class.getDeclaredField("mFromLayout"); field.setAccessible(true); field.setBoolean(this, true); field.setAccessible(false); </code></pre> <p>As I said, not a real solution, but a way to make it work before someone finds a proper solution.</p> <p>[UPDATE] This method will not work with Robolectric 2.0 Final</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload