2

我有 GridLayout 作为子视图,设置(空)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

为什么?

4

2 回答 2

3

您是否将 a 传递GridLayout.LayoutParamsGridLayout自身?我不相信这是对的。

当您调用 时setLayoutParams,您正在向父类提供有关如何布局特定子类的信息。您应该将 a 传递<ParentClass>.LayoutParams给 GridLayout,GridLayout 所在ParentClass的容器在哪里。

GridLayout.LayoutParams仅用于为a的GridLayout级设置 layoutParams 。

于 2012-07-15T17:02:12.033 回答
2

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

于 2012-07-15T18:03:29.210 回答