FrameLayout to RelativeLayout ClassCastException 即使没有使用 FrameLayout
Posted
技术标签:
【中文标题】FrameLayout to RelativeLayout ClassCastException 即使没有使用 FrameLayout【英文标题】:FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used 【发布时间】:2012-07-17 16:48:44 【问题描述】:在我的应用程序中,我有一个布局,它有一个 RelativeLayout
,我想在运行时以编程方式为其设置边距。但是当我这样做时,它给了我ClassCastException
说FrameLayout
不能转换为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_
android:layout_ >
<RelativeLayout
android:id="@+id/ll_lhs_menu"
android:layout_
android:layout_
android:background="@color/menu_bg"
android:orientation="vertical">
.....
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_right"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:background="@drawable/capture_port"
android:scrollbars="none" >
....
</RelativeLayout>
</RelativeLayout>
这是我的 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);
这里是 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)
我哪里错了?
【问题讨论】:
【参考方案1】:如果是 RelativeLayout.LayoutParams,请尝试设置 FrameLayout.LayoutParams。在运行时设置布局参数时,必须从它的父级设置。
所以,它会是:
FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
_rootLayoutParams.setMargins(300, 0, 300, 0);
_rootLayout.setLayoutParams(_rootLayoutParams);
【讨论】:
不。ViewGroup.LayoutParams
没有任何 setMargins()
方法。如果我再次将其转换为 MarginLayoutParams
,则会发生不同的 classCastException。
你试过FrameLayout.LayoutParams吗?由于您的 logCat 错误,这可能是正确的。查看我的编辑。
嘿,FrameLayout.LayoutParams
删除了 ClassCastException 但它没有显示任何 UI。
你必须检查 _rootLayoutParams 是如何初始化的。您似乎正在设置 _rootLayout 的宽度和高度,但我在您的代码中没有看到该对象。
是的,它就在代码_rootLayout = (RelativeLayout) findViewById(R.id.rl_root);
之前忘了把它放在问题中。更新。但是为什么要FrameLayout.LayoutParams
?【参考方案2】:
我之前遇到过一次此错误,这是因为我尝试强制转换参数而不是创建新实例。我希望这会有所帮助。
就在这里:
RelativeLayout.LayoutParams _rootLayoutParams = new RelativeLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
应该是:
RelativeLayout.LayoutParams _rootLayoutParams = new RelativeLayout.LayoutParams(_rootLayout.getLayoutParams());
现在您已经获得了父(根)布局的所有布局参数,您可以使用它们来为您正在膨胀的子视图设置参数。
// 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);
【讨论】:
这里的viewGroup是什么?【参考方案3】:我建议@id/rl_root 的父级必须是未显示在此xml 中的FrameLayout。 LayoutParam 的类型应该和它的 parent 相同,而不是它自己。
【讨论】:
以上是关于FrameLayout to RelativeLayout ClassCastException 即使没有使用 FrameLayout的主要内容,如果未能解决你的问题,请参考以下文章