ClassCastException android.widget.AbsListView $ LayoutParams到android.widget.GridLayout $ LayoutParams是特定于API级别的这个例外吗?

时间:2014-09-04 13:03:20

标签: android android-layout layout layoutparams

我试图动态(膨胀)LinearLayout添加到GridLayout,如下图所示。在GridLayout"行"内ListView是静态的布局。

当我尝试将LinearLayout添加到GridLayout时,我收到了错误消息:

09-01 12:55:54.320: E/AndroidRuntime(4156): FATAL EXCEPTION: main
09-01 12:55:54.320: E/AndroidRuntime(4156): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.GridLayout$LayoutParams
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.GridLayout.getLayoutParams1(GridLayout.java:729)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.GridLayout.validateLayoutParams(GridLayout.java:663)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.GridLayout.getLayoutParams(GridLayout.java:734)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:866)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.GridLayout.onMeasure(GridLayout.java:894)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.ListView.measureScrapChild(ListView.java:1181)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.ListView.onMeasure(ListView.java:1155)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4706)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.View.measure(View.java:12922)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1255)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2649)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.os.Looper.loop(Looper.java:137)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at android.app.ActivityThread.main(ActivityThread.java:4507)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at java.lang.reflect.Method.invoke(Method.java:511)
09-01 12:55:54.320: E/AndroidRuntime(4156):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)

如堆栈跟踪中所述,AbsListView $ LayoutParams无法转换为GridLayout $ LayoutParams。我不知道为什么会这样?

注意:怪异部分仅在 4.0.4 API 15 设备中发生。它在4.3和4.4中运行良好

version_4.0.4_api_15

更新:(示例代码)

viewHolder.gridLayout1 = (GridLayout) getView().findViewById(R.id.sampleGridLayout);
setLinear(viewHolder);

private void setLinear(ViewHolder viewHolder) {
    viewHolder.gridLayout1.removeAllViews();

    for (int i = 0; i < 3; i++) {
        LinearLayout matchLayout = (LinearLayout) viewHolder.inflater.inflate(R.id.sampleLinear, parent, false);
        //view populations in matchLayout

        viewHolder.gridLayout1.addView(matchLayout);
    }
}

1 个答案:

答案 0 :(得分:2)

问题是您传递给parent的{​​{1}} ViewGroup是ListView,而不是GridLayout。

由于您的适配器用于填充ListView,传入viewHolder.inflater.inflate();的{​​{1}}将是ListView。

当LayoutInflater膨胀你的parent布局时,它会给出膨胀的布局getView(int position, View convertView, ViewGroup parent),因为你告诉inflater你打算将布局放在某种类型的AbsListView中。

当您尝试将该布局放入GridLayout时,这将导致您收到的错误,这需要所有孩子都使用R.id.sampleLinear

在旁注中,您应该将布局(以AbsListView.LayoutParams)的形式而不是ID(例如GridLayout.LayoutParams)传递给您的inflater。我将假设这是一个错字,否则你会得到一个完全不同的例外。