Android FrameLayout to RelativeLayout ClassCastException 即使没有使用 FrameLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11544964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used
提问by Rajkiran
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:
在我的应用程序中,我有一个布局RelativeLayout
,我想在运行时以编程方式为其设置边距。但是当我这样做时,它让我ClassCastException
说FrameLayout
can not cast to RelativeLayout
。我没有使用任何 FrameLayout,也没有FrameLayout
. 问题仍然存在。我使用的 xml 是:
<?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>
And this is my onCreate where I set the margins to the parent layout:
这是我的 onCreate,我将边距设置为父布局:
/** 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);
}
And here is the LogCat:
这是 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?
我哪里错了?
回答by I?igo
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.
如果 RelativeLayout.LayoutParams,请尝试设置 FrameLayout.LayoutParams。当您在运行时设置布局参数时,您必须从它的父级设置布局参数。
So, it'll be:
所以,它将是:
FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
_rootLayoutParams.setMargins(300, 0, 300, 0);
_rootLayout.setLayoutParams(_rootLayoutParams);
回答by Holyghost Moneyshot
I've had this error once before and it was because I tried to cast the parameters instead of creating a new instance. I hope this helps.
我以前遇到过这个错误,这是因为我试图强制转换参数而不是创建一个新实例。我希望这有帮助。
Right here :
就在这儿 :
RelativeLayout.LayoutParams _rootLayoutParams = new RelativeLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
Should be:
应该:
RelativeLayout.LayoutParams _rootLayoutParams = new RelativeLayout.LayoutParams(_rootLayout.getLayoutParams());
Now that you have all the layout parameters from the parent (root) layout, you can use them to set the parameters for your child view that you are inflating.
现在您拥有父(根)布局中的所有布局参数,您可以使用它们为您正在膨胀的子视图设置参数。
// Example usage:
// viewGroup is the parent in this case whose parameters we want as a reference point.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(viewGroup.getLayoutParams());
// Now we can add a rule stating that we want to center our new view both horizontally and vertically in the parent (viewGroup) view.
params.addRule(RelativeLayout.CENTER_IN_PARENT);
// Add more rules here if you would like
params.addRule(newRule2);
// After all of your new parameters are laid out, assign them to the view you want them applied to
newView.setLayoutParams(params);
// Add the view to your hierarchy and enjoy
viewGroup.addView(newView);
回答by Shengfeng Li
I suggest that the parent of the @id/rl_root must be FrameLayout which is not showing in this xml. LayoutParam's type should be the same with its parent not itself.
我建议@id/rl_root 的父级必须是 FrameLayout,它没有显示在这个 xml 中。LayoutParam 的类型应该与其父级相同,而不是本身。