的FrameLayout到RelativeLayout的ClassCastException异常即使没有使用的FrameLayout [英] FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used
问题描述
在我的应用程序,我有一个布局具有 RelativeLayout的
来我希望以编程方式设置页边距在运行时它。但是,当我做到这一点,它给了我 ClassCastException异常
说的FrameLayout
无法转换为 RelativeLayout的
。我没有任何的FrameLayout使用,也没有进口的FrameLayout
。不过问题依然存在。我使用的XML是:
In my application, I have a layout which has a RelativeLayout
to which I want to set margins at runtime programmatically. But when I do that, it gives me ClassCastException
saying FrameLayout
can not cast to RelativeLayout
. I don't have any FrameLayout used, also there are no imports for FrameLayout
. Still the problem persists. The xml I am using is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_root"
android:background="@color/menu_bg"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/ll_lhs_menu"
android:layout_width="300dip"
android:layout_height="fill_parent"
android:background="@color/menu_bg"
android:orientation="vertical">
.....
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@drawable/capture_port"
android:scrollbars="none" >
....
</RelativeLayout>
</RelativeLayout>
这是我的onCreate,我设置页边距到父布局:
And this is my onCreate where I set the margins to the parent layout:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
_rootLayout = (RelativeLayout) findViewById(R.id.rl_root);
RelativeLayout.LayoutParams _rootLayoutParams = new RelativeLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
_rootLayoutParams.setMargins(300, 0, 300, 0);
_rootLayout.setLayoutParams(_rootLayoutParams);
}
这里是LogCat中:
And here is the LogCat:
07-18 21:12:39.410: E/AndroidRuntime(7663): FATAL EXCEPTION: main
07-18 21:12:39.410: E/AndroidRuntime(7663): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.widget.FrameLayout.onMeasure(FrameLayout.java:268)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.view.View.measure(View.java:10828)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.view.View.measure(View.java:10828)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4355)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
07-18 21:12:39.410: E/AndroidRuntime(7663): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1912)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.view.View.measure(View.java:10828)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.view.ViewRoot.performTraversals(ViewRoot.java:960)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.view.ViewRoot.handleMessage(ViewRoot.java:2062)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.os.Looper.loop(Looper.java:132)
07-18 21:12:39.410: E/AndroidRuntime(7663): at android.app.ActivityThread.main(ActivityThread.java:4128)
07-18 21:12:39.410: E/AndroidRuntime(7663): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 21:12:39.410: E/AndroidRuntime(7663): at java.lang.reflect.Method.invoke(Method.java:491)
07-18 21:12:39.410: E/AndroidRuntime(7663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-18 21:12:39.410: E/AndroidRuntime(7663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-18 21:12:39.410: E/AndroidRuntime(7663): at dalvik.system.NativeStart.main(Native Method)
我在哪里去了?
Where am I going wrong?
推荐答案
尝试设置FrameLayout.LayoutParams如果不是RelativeLayout.LayoutParams。当您设置布局PARAMS在运行时,你必须设置那些从它的母公司。
Try setting FrameLayout.LayoutParams instead if RelativeLayout.LayoutParams. When you set the layout params at runtime, you have to set the ones from it's parent.
所以,这将是:
FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
_rootLayoutParams.setMargins(300, 0, 300, 0);
_rootLayout.setLayoutParams(_rootLayoutParams);
这篇关于的FrameLayout到RelativeLayout的ClassCastException异常即使没有使用的FrameLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!