androidclasscastexceptionlayoutparams

Handling ClassCastException when we set LayoutParams programmatically


I am doing like this -

OPTION 1:

Setting params first time:

FrameLayout fLayout = (FrameLayout) getActivity().
findViewById(R.id.headlines_fragment); 
fLayout.setLayoutParams(new LinearLayout.
LayoutParams(310, LinearLayout.LayoutParams.MATCH_PARENT));

Setting params second time:

FrameLayout fLayout = (FrameLayout) getActivity().
findViewById(R.id.headlines_fragment); 
fLayout.setLayoutParams(new LinearLayout.
LayoutParams(500, LinearLayout.LayoutParams.MATCH_PARENT, .03f));

Although I am setting the LayoutParams in my other parts of application as well but my suspect was the above code.

So I changed the above code to be -

OPTION 2:

Setting params first time:

FrameLayout fLayout = (FrameLayout) getActivity().
findViewById(R.id.headlines_fragment); 
fLayout.setLayoutParams(new FrameLayout.LayoutParams(310,
FrameLayout.LayoutParams.MATCH_PARENT));

Setting params second time:

FrameLayout fLayout = (FrameLayout) getActivity().
findViewById(R.id.headlines_fragment); 
fLayout.setLayoutParams(new FrameLayout.LayoutParams(500, 
FrameLayout.LayoutParams.MATCH_PARENT, .03f));

However, setting the param second time in OPTION 2 is giving the compile time exception -

The constructor FrameLayout.LayoutParams(int, int, float) is undefined

So my two questions -

1) Am I doing right to set the layout params to FrameLayout i.e following OPTION 2 instead of OPTION 1 ?

2) How to remove this compile time error?

My XML -

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/headlines_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.25" />

    <FrameLayout
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

My Log -

Stack Trace- java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.view.ViewPager$LayoutParams
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1319)
at android.view.View.measure(View.java:15609)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15609)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1041)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
at android.view.View.measure(View.java:15609)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

Solution

  • 1) you are doing wrong, there is no such constructor for FrameLayout.LayoutParams. constructor for LinearLayout.LayoutParams which takes int, int, float is for weight setting (last float value), FrameLayout don't support weights

    2) if you really need this weight .03f you have to use LinearLayout.LayoutParams or eventually you might calculate (measure) height/width and in second initiation of params use this value instead wrap_content/match_parent