我使用了一个包含 RelativeLayout -> ScrollView -> LinearLayout 的视图。我遇到了异常:
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。
EditText input = new EditText(context);
看起来代码是动态创建视图?那么我们肯定需要看更多的代码。我们不能确定视图是否按照给定的布局层次结构添加/创建。很抱歉,如果没有更多的代码可供查看,我们无法提供进一步的帮助。我希望LinearLayout.LayoutParams
解决了问题。 - Jarno Argillander