GridLayout android.view.ViewGroup $ LayoutParams无法强制转换为android.support.v7.widget.GridLayout $ LayoutParams

时间:2012-07-15 14:20:15

标签: android grid-layout android-2.3-gingerbread

我将GridLayout作为子视图,使用set(空)GridLayout.LayoutParams

setLayoutParams(new GridLayout.LayoutParams());

我没有在其他地方设置LayoutParams。但我得到了异常

E/AndroidRuntime( 1620): FATAL EXCEPTION: main
E/AndroidRuntime( 1620): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.GridLayout$LayoutParams
E/AndroidRuntime( 1620):    at android.support.v7.widget.GridLayout.computeLayoutParamsHashCode(GridLayout.java:872)
E/AndroidRuntime( 1620):    at android.support.v7.widget.GridLayout.checkForLayoutParamsModification(GridLayout.java:879)
E/AndroidRuntime( 1620):    at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:927)
E/AndroidRuntime( 1620):    at android.view.View.measure(View.java:15172)
E/AndroidRuntime( 1620):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1249)
E/AndroidRuntime( 1620):    at android.view.View.measure(View.java:15172)
E/AndroidRuntime( 1620):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
E/AndroidRuntime( 1620):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
E/AndroidRuntime( 1620):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime( 1620):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime( 1620):    at android.view.View.measure(View.java:15172)
E/AndroidRuntime( 1620):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
E/AndroidRuntime( 1620):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 1620):    at android.view.View.measure(View.java:15172)
E/AndroidRuntime( 1620):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
E/AndroidRuntime( 1620):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
E/AndroidRuntime( 1620):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime( 1620):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime( 1620):    at android.view.View.measure(View.java:15172)
E/AndroidRuntime( 1620):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
E/AndroidRuntime( 1620):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime( 1620):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
E/AndroidRuntime( 1620):    at android.view.View.measure(View.java:15172)
E/AndroidRuntime( 1620):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
E/AndroidRuntime( 1620):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
E/AndroidRuntime( 1620):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
E/AndroidRuntime( 1620):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
E/AndroidRuntime( 1620):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
E/AndroidRuntime( 1620):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
E/AndroidRuntime( 1620):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
E/AndroidRuntime( 1620):    at android.view.Choreographer.doFrame(Choreographer.java:525)
E/AndroidRuntime( 1620):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
E/AndroidRuntime( 1620):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime( 1620):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1620):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1620):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 1620):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1620):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 1620):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 1620):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 1620):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  304):   Force finishing activity com.capsule.launcher/.LauncherActivity

为什么?

2 个答案:

答案 0 :(得分:3)

您是否将GridLayout.LayoutParams传递给GridLayout本身?我不相信这是对的。

当您致电setLayoutParams时,您将向父类提供有关如何布置特定孩子的信息。您应该将<ParentClass>.LayoutParams传递给GridLayout,其中ParentClass是GridLayout所在的容器。

GridLayout.LayoutParams仅用于为GridLayout孩子设置layoutParams。

答案 1 :(得分:2)

您的应用使用哪个版本的Android Api?看起来它使用Api 14或以上,你也使用Android支持库。当您尝试从支持库将GridLayout.LayoutParams()从Api 14传递到GridLayout.setLayoutParams时,可能会出现错误。请检查您的进口。