0

我正在开发一个最低版本为 2.2 的 android 应用程序。我也在使用 android support v7 gridlayout。我尝试在 java 代码中创建它并将其插入到线性布局中。但是,当我这样做时它会崩溃。

    GridLayout gl = new GridLayout(this);
    LinearLayout.LayoutParams gridMargin = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    gl.setLayoutParams(gridMargin);
    gl.setColumnCount(1);
    gl.setRowCount(1);

            TextView number = new TextView(this);
            number.setPadding(10, 10, 10, 10);
            number.setText(Integer.toString(1, 10));
            number.setTextAppearance(this, R.style.studentGrid);
            number.setBackgroundResource(R.drawable.grid_cell);
            LinearLayout.LayoutParams numberMargin = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            number.setLayoutParams(numberMargin);
            gl.addView(number);

    studentContainer.addView(gl);

如果我不添加子元素,那么它不会崩溃。有谁知道这里有什么问题?

谢谢。

10-22 20:33:06.160: E/AndroidRuntime(25131): FATAL EXCEPTION: main
10-22 20:33:06.160: E/AndroidRuntime(25131): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.GridLayout$LayoutParams
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.support.v7.widget.GridLayout.computeLayoutParamsHashCode(GridLayout.java:872)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.support.v7.widget.GridLayout.checkForLayoutParamsModification(GridLayout.java:879)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:927)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.HorizontalScrollView.measureChildWithMargins(HorizontalScrollView.java:1475)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.HorizontalScrollView.onMeasure(HorizontalScrollView.java:361)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1466)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.ScrollView.onMeasure(ScrollView.java:388)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5108)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.View.measure(View.java:15460)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1987)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1401)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.Choreographer.doFrame(Choreographer.java:525)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.os.Handler.handleCallback(Handler.java:615)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.os.Looper.loop(Looper.java:137)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at android.app.ActivityThread.main(ActivityThread.java:4950)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at java.lang.reflect.Method.invokeNative(Native Method)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at java.lang.reflect.Method.invoke(Method.java:511)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-22 20:33:06.160: E/AndroidRuntime(25131):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

当您将布局参数设置为子小部件时,布局参数实现类必须与容器小部件匹配。换句话说,如果要将子级添加到 aGridLayout中,则必须使用 setGridLayout.LayoutParams到子级中。

例如,

GridLayout.LayoutParams numberMargin = new GridLayout.LayoutParams(...);
number.setLayoutParams(numberMargin);
于 2013-10-23T00:40:06.437 回答