gpt4 book ai didi

android - FrameLayout to RelativeLayout ClassCastException 即使没有使用 FrameLayout

转载 作者:IT老高 更新时间:2023-10-28 23:19:28 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个布局,它有一个 RelativeLayout 我想在运行时以编程方式设置边距。但是当我这样做时,它给了我 ClassCastExceptionFrameLayout 不能转换为 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>

这是我的 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)

我哪里错了?

最佳答案

如果是 RelativeLayout.LayoutParams,请尝试设置 FrameLayout.LayoutParams。在运行时设置布局参数时,必须从其父级设置。

所以,它会是:

FrameLayout.LayoutParams _rootLayoutParams = new FrameLayout.LayoutParams(_rootLayout.getWidth(), _rootLayout.getHeight());
_rootLayoutParams.setMargins(300, 0, 300, 0);
_rootLayout.setLayoutParams(_rootLayoutParams);

关于android - FrameLayout to RelativeLayout ClassCastException 即使没有使用 FrameLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11544964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com