17

My initial code line is inside the overridden method onGlobalLayout():

img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 100));

ClassCastException occuring in the overridden method onGlobalLayout()-

Error Log:

09-02 14:25:35.326: E/AndroidRuntime(5187): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v4.view.ViewPager$LayoutParams
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1319)
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.view.View.measure(View.java:15181)
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.view.View.measure(View.java:15181)
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-02 14:25:35.326: E/AndroidRuntime(5187):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)

And when I do the following in order to resolve the above error as indicated in other SO posts:

img.setLayoutParams(new ViewPager.LayoutParams(ViewPager.LayoutParams.MATCH_PARENT, 100));

Then, I get the compile time error:

The constructor ViewPager.LayoutParams(int, int) is undefined

ViewPager in xml is defined like:

<android.support.v4.view.ViewPager
 android:id="@+id/HView"
 android:layout_width="560dp"
 android:layout_height="255dp"
 android:layout_centerHorizontal="true"
 android:layout_marginLeft="160sp"
 android:layout_marginTop="110sp"
 android:layout_marginBottom="80sp">
 </android.support.v4.view.ViewPager>
sjain
  • 23,126
  • 28
  • 107
  • 185
  • http://developer.android.com/reference/android/support/v4/view/ViewPager.LayoutParams.html. check the constructors. `public ViewPager.LayoutParams (Context context, AttributeSet attrs)` your params are wrong no constructor that takes int int as param – Raghunandan Sep 02 '13 at 09:02
  • check your imports please – Lia Pronina Sep 02 '13 at 09:03
  • Ok I did checked that but then How to resolve it. – sjain Sep 02 '13 at 09:03
  • @LiaPronina - `android.support.v4.view.ViewPager` – sjain Sep 02 '13 at 09:05
  • what is this `img`? where is this in XML, whats the parent of this `img` – Faizan Sep 02 '13 at 09:06
  • try to use ViewGroup instead ViewPager in using LayoutParams – Lia Pronina Sep 02 '13 at 09:08
  • img - `final ImageView img = new ImageView(getActivity());` Its parent is also dynamically created as: `linearlayout = new LinearLayout(getActivity()); linearlayout.addView(img);`. This is all inside `android.support.v4.view.ViewPager` in the xml. – sjain Sep 02 '13 at 09:09
  • Ok try `img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100));` – Faizan Sep 02 '13 at 09:13
  • I tried all those combinations same error. – sjain Sep 02 '13 at 09:17

3 Answers3

31

Your image already has layout params. Instead of replacing them with a new object (which is of wrong type in this case, ViewPager expects them to be ViewPager.LayoutParams), you can modify existing layout params like this:

ViewGroup.LayoutParams params = img.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = 100;
img.requestLayout();

Note that unconditionally requesting re-layout in onGlobalLayout() can be a bad idea, leading to infinite layout passes. I haven't verified it myself whether this is the case.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • This is causing a loop. I tried to put my code in question just after `onGlobalLayout()` and its working fine but I noticed that my code is getting called before `onGlobalLayout()`. This appears strange just like threads do. So I am bound to put it inside `onGlobalLayout()`. – sjain Sep 02 '13 at 09:28
  • Yes - could you clarify why you need to modify the layout in a global layout listener? There's probably a better way. (This answer addresses the class cast exception issue.) – laalto Sep 02 '13 at 09:31
  • Because I am trying to get the end position of text view and image view. See my this SO post and my own accepted answer there- `http://stackoverflow.com/questions/18533224/getting-end-position-of-the-textview-and-imageview-with-respect-to-top-of-the-sc` – sjain Sep 02 '13 at 09:48
  • See I modified my own answer just now to include the img code there. – sjain Sep 02 '13 at 09:53
  • Thanks, I am able to stop the looping by, `view.getViewTreeObserver().removeOnGlobalLayoutListener(this);`. – sjain Sep 03 '13 at 07:39
  • Worked perfectly for me! – Michael Aug 06 '14 at 22:27
  • the reason why I am trying to create a new object is because .getLayoutParams() is returning null, so what do I do in that case? – Shay Ribera Sep 20 '21 at 13:49
0

Do this way

img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));

UPDATE

 LayoutParams params = img.getLayoutParams();
         params.width = LayoutParams.MATCH_PARENT;
         params.height = 100;
         img.setLayoutParams(params);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • I already checked that too. Its giving the same exception but this time with the following log: `09-02 14:37:23.953: E/AndroidRuntime(5822): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v4.view.ViewPager$LayoutParams ` – sjain Sep 02 '13 at 09:08
  • This is working fine but causing a loop. I tried to put my code in question just after onGlobalLayout() and its working fine but I noticed that my code is getting called before onGlobalLayout(). This appears strange just like threads do. So I am bound to put it inside onGlobalLayout(). – sjain Sep 02 '13 at 09:29
0

You should make sure the LayoutParam class you are using is from the correct parent Layout class. And you need to set the LayoutParams of the ViewGroup the ViewPager is sitting in, as the parent of the View that needs to know what size to allocate to the child View. For example, in your case the ViewGroup is a RelativeLayout, you need to use the RelativeLayout.LayoutParams class.

You can try following:

img.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 100));
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72