以编程方式设置LayoutParams时处理ClassCastException

时间:2014-12-11 09:46:46

标签: android classcastexception layoutparams

我这样做 -

选项1:

第一次设置参数:

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

第二次设置参数:

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

虽然我在应用程序的其他部分也设置了LayoutParams,但我怀疑是上面的代码。

所以我将上面的代码更改为 -

选项2:

第一次设置参数:

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

第二次设置参数:

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

但是,在OPTION 2中设置参数第二次是给出编译时异常 -

构造函数FrameLayout.LayoutParams(int,int,float)未定义

所以我的两个问题 -

1)我是否正确将布局参数设置为FrameLayout,即以下内容    选项2而不是选项1?

2)如何删除此编译时错误?

我的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>

我的日志 -

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)

1 个答案:

答案 0 :(得分:0)

1)你做错了,FrameLayout.LayoutParams没有这样的构造函数。 LinearLayout.LayoutParams的构造函数int, int, float用于weight设置(最后一个浮点值),FrameLayout不支持权重

2)如果你真的需要这个重量.03f,你必须使用LinearLayout.LayoutParams或最终你可以计算(测量)高度/宽度,并且在第二次启动参数时使用这个值而不是{{1} } / wrap_content