< Edit >
-
A new observation – I’ve tried the same ViewPager as stand alone Activity and as an AppWidget configuration view – and it crashes often as configuration view, while consistently runs well as a simple activity.
-
Another observation – If the configuration layout is ViewPager as root, no crashes happened. if the ViewPager is embedded in FlowLayout or RelativeLayout – it crashes often.
< / Edit >
I’m using v4 ViewPager with PagerTabStrip in my AppWidget configuration xml, and it works perfectly most of the time.
However, I get this error (app crash) when some processing involved while loading the view:
java.lang.IllegalStateException: Must measure with an exact width
at android.support.v4.view.PagerTitleStrip.onMeasure(PagerTitleStrip.java:428)
at android.view.View.measure(View.java:8335)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1350)
at android.view.View.measure(View.java:8335)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
...etc...
A code snippet from android.support.v4.view.PagerTitleStrip.onMeasure() –
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//...
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("Must measure with an exact width");
}
//...
}
How could I control the widthMode??
All the stack above is happening outside my code.
In my code I’ve tried to rule out many factors, and eventually I may have managed to isolate a root cause:
When I’m adding a mock “work” of 5 milis sleep per fragment (X30 fragments), I get the above error, and when removing it – the error seem gone.
Tried moving the work to onCreate() – didn’t make a difference
public static class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.splash, container, false);
try {
Thread.sleep(5); // << mock work causes quite consistent error
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "..onCreateView");
return v;
}
}
The actual work I’m doing on my real adapter’s fragment is minimal and can’t be avoided (setting the initial values which later populate the data in a background thread). So I don’t know how much more I can defer to a background thread.
Does anyone have experience with this?
Any idea why the above error occurs, and if it is possible to avoid it?
For reference (Not sure if it’s relevant):
<LinearLayout ...>
<android.support.v4.view.ViewPager
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="@+id/aw_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:background="@drawable/dialog_full_holo_dark"
android:fadingEdge="horizontal"
android:fadingEdgeLength="40dp" >
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</LinearLayout>
Solved at last! (2 days wasted on this):
it was this line:
I replaced it with:
and the problem is gone.
Nothing in the error has suggested that, and it wasn’t deterministic. Solved the hard way by elimination – add code > test > again…
It is most likely a bug in Android code, probably some race condition in View layout management.
Hope I saved 2 days for fellow devs bumping here.
[Update]
In another project I worked around this bug by wrapping the
ViewPagerwith aFrameLayout. I set the background I needed to theFrameLayout, and set@color/transparentto theViewPager.