Android 动态设置布局文件的exception

错误信息:

 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
     at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
     at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
     at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
     at android.view.View.measure(View.java:12728)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
     at android.view.View.measure(View.java:12728)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
     at android.view.View.measure(View.java:12728)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2095)
     at android.view.View.measure(View.java:12728)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:4426)
     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:788)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
     at dalvik.system.NativeStart.main(Native Method)

解决办法:

如果你要将一个view添加到另一个布局中,你必须设定该View的布局参数为其父类所使用的布局参数类型。

RelativeLayout.LayoutParams layoutParams=new RelativeLayout.
                LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        layoutParams.setMargins(0, 120, 0, 0);

转载于:https://my.oschina.net/arrom/blog/224958

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值