0

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Why you changed from first option to second option? ie from LinearLayout layoutparams to Framelayout params? – Darpan Dec 11 '14 at 09:58
  • I changed from first option to second because I am getting `ClassCastException` somewhere in my code but don't know where. I thought that its my first option that is causing it and so I changed it to second option i.e. from LinearLayout layoutparams to Framelayout params. – sjain Dec 11 '14 at 10:00
  • First code can't be the reason of classcast exception. Check http://stackoverflow.com/a/8569947/609782 – Darpan Dec 11 '14 at 10:03

1 Answers1

0

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

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • See my updated XML. I was setting the layout params of `FrameLayout `to `LinearLayout` by using `new LinearLayout.LayoutParams`. Is it wrong ? – sjain Dec 11 '14 at 09:58
  • you should set `LayoutParams` of parent, not the view for which you are setting. so if your `headlines_fragment` is still inside `LinearLayout` then you should use `LinearLayout.LayoutParams`. LayourParams corresponds to `android:layout_blahblah` (width, margin etc.) xml params and they are used to inflate view in it's parent, so params should correspond to parent params form for unique values like `layout_centerHorizontal` for `RelativeLayout`. if you want only width and height you might use `ViewGroup.LayoutParams` (all `LayoutParams` extends it) – snachmsm Dec 11 '14 at 10:01