gpt4 book ai didi

java.lang.ClassCastException : GridLayout. LayoutParams 无法转换为 Frame.LayoutParams

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:29 24 4
gpt4 key购买 nike

带有 Android Studio 2.0 的 OS X Yosemite 10.10.5

出于教学目的,我想将尽可能多的 View 属性从 XML 移动到 Java。

我在 GridLayout 中有一个 android.support.v7.widget.RecyclerView,我想移动 RecyclerView 的 android:layout_gravity .

GridLayout.setGravityR.attr 文档我知道 layout_gravity 被子级用来设置父级的重力属性。

以下代码是异常的根本原因:

GridLayout.LayoutParams parentParams = new GridLayout.LayoutParams();
parentParams.setGravity(Gravity.CENTER_VERTICAL);
container.setLayoutParams(parentParams);

我要收缩的 XML:

<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/alphabet_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--android:layout_gravity="center_vertical">-->
</android.support.v7.widget.RecyclerView>

</GridLayout>

这是异常链:

java.lang.ClassCastException: android.widget.GridLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
at android.widget.FrameLayout.onMeasure(FrameLayout.java:431)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我查找了 android.view.View.measure(View.java:17430) 找到了这个:

if (cacheIndex < 0 || sIgnoreMeasureCache) {
// measure ourselves, this should set the measured dimension flag back
onMeasure(widthMeasureSpec, heightMeasureSpec);
mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
} else {

我真的不明白它最终是如何调用 android.support.v7.widget.ContentFrameLayout.onMeasure 而不是 android.view.View.onMeasure

我错过了什么?

最佳答案

在您提供的代码 fragment 中:

GridLayout.LayoutParams parentParams = new GridLayout.LayoutParams();
parentParams.setGravity(Gravity.CENTER_VERTICAL);
container.setLayoutParams(parentParams);

出现从变量名container,你正试图将你构造的LayoutParams应用到你的GridLayout 实例,而不是你的 RecyclerView 实例。我希望最后一行看起来更像

recyclerView.setLayoutParams(parentParams);

如果我的假设是正确的,您的 GridLayout 的父级将最终尝试解析您构建的 LayoutParams。这个父级本身不太可能(!)是一个 GridLayout(我从堆栈跟踪中猜测它是一个 FrameLayout),并且在它试图处理GridLayout-特定的LayoutParams

关于java.lang.ClassCastException : GridLayout. LayoutParams 无法转换为 Frame.LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36683495/

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