gpt4 book ai didi

android - java.lang.ClassCastException : android. view.ViewGroup$LayoutParams 没有使用 LayoutParams

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

我使用一个具有RelativeLayout -> ScrollView -> LinearLayout 的 View 。我得到了异常(exception):

11-22 11:17:24.605: ERROR/AndroidRuntime(5161): FATAL EXCEPTION: main
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3131)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.view.View.measure(View.java:8172)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.os.Looper.loop(Looper.java:144)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at android.app.ActivityThread.main(ActivityThread.java:4937)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at java.lang.reflect.Method.invoke(Method.java:521)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 11:17:24.605: ERROR/AndroidRuntime(5161): at dalvik.system.NativeStart.main(Native Method)

为什么它只是偶尔发生?为什么没有代码失败的细节?

更新:我怀疑它应该围绕此代码失败:

EditText input = new EditText(context);
input.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

LayoutParams 是 RelativeLayout.LayoutParams

最佳答案

使用 new LinearLayout.LayoutParams(...) 而不是 new LayoutParams(...)

LayoutParams 总是需要使用父布局的类。例如。如果 EditTextFrameLayout 内,则必须使用 FrameLayout.LayoutParams

关于android - java.lang.ClassCastException : android. view.ViewGroup$LayoutParams 没有使用 LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8226676/

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