ListView的一个典型crash cannot be cast to android.widget.AbsListView$LayoutParams

1. 背景

一个新版本的代码,在4.x版本进入某个页面的时候,必现crash。看到必现,心情就放松了一半。
大致的crash信息如下:

FATAL EXCEPTION: main
java.lang.ClassCastException: android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
 at android.widget.ListView.setupChild(ListView.java:1826)
 at android.widget.ListView.makeAndAddView(ListView.java:1793)
 at android.widget.ListView.fillDown(ListView.java:691)
 at android.widget.ListView.fillSpecific(ListView.java:1349)
 at android.widget.ListView.layoutChildren(ListView.java:1608)
 at android.widget.AbsListView.onLayout(AbsListView.java:2091)
 ....

其实还有很多类似的crash堆栈,一google,发现一大片。https://stackoverflow.com/questions/25666274/classcastexception-android-widget-abslistviewlayoutparams-to-android-widget-gri

2. 为什么会出现crash

测试的时候,发现5.x不会crash,4.x必然重现是什么原因呢?
我们发现栈顶setupChild,先找该函数:

http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/widget/ListView.java#setupChild

private void setupChild(View child, int position, int y, boolean flowDown, int childrenLeft,
1887            boolean selected, boolean recycled) {
。。。。。
1898        // Respect layout params that are already in the view. Otherwise make some up...
1899        // noinspection unchecked
1900        AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams();
1901        if (p == null) {
1902            p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
1903        }
1904        p.viewType = mAdapter.getItemViewType(position);
1905
1906        if ((recycled && !p.forceAdd) || (p.recycledHeaderFooter &&
1907                p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER)) {
1908            attachViewToParent(child, flowDown ? -1 : 0, p);
1909        } else {
1910            p.forceAdd = false;
1911            if (p.viewType == AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER) {
1912                p.recycledHeaderFooter = true;
1913            }
1914            addViewInLayout(child, flowDown ? -1 : 0, p, true);
1915        }
。。。。
1972    }
1973

我们看到 AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams() 一句正是crash的根源,强制转换导致的crash。 这里对比4.x与5.x版本的源码,发现两个版本的这里没有什么区别。 那是什么情况导致的child差异呢?跟踪代码,回到上级调用makeAndAddView里,看下源码两个版本基本一致的。

3. makeAndAddView

http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/widget/ListView.java#makeAndAddView

private View makeAndAddView(int position, int y, boolean flow, int childrenLeft,
1847            boolean selected) {
1848        View child;
1849
1850
1851        if (!mDataChanged) {
1852            // Try to use an existing view for this position
1853            child = mRecycler.getActiveView(position);
1854            if (child != null) {
1855                // Found it -- we're using an existing child
1856                // This just needs to be positioned
1857                setupChild(child, position, y, flow, childrenLeft, selected, true);
1858
1859                return child;
1860            }
1861        }
1862
1863        // Make a new view for this position, or convert an unused view if possible
1864        child = obtainView(position, mIsScrap);
1865
1866        // This needs to be positioned and measured
1867        setupChild(child, position, y, flow, childrenLeft, selected, mIsScrap[0]);
1868
1869        return child;
1870    }

我们看到child = obtainView(position, mIsScrap)来自与listView的缓存相关。所以还是得跟踪obtainView,该函数也很好理解,有缓存的时候,从缓存池中取,否则重新生成。

4. obtainView

先看5.0版本的代码

2304    /**
2305     * Get a view and have it show the data associated with the specified
2306     * position. This is called when we have already discovered that the view is
2307     * not available for reuse in the recycle bin. The only choices left are
2308     * converting an old view or making a new one.
2309     *
2310     * @param position The position to display
2311     * @param isScrap Array of at least 1 boolean, the first entry will become true if
2312     *                the returned view was taken from the scrap heap, false if otherwise.
2313     *
2314     * @return A view displaying the data associated with the specified position
2315     */
2316    View obtainView(int position, boolean[] isScrap) {
。。。
2321        // Check whether we have a transient state view. Attempt to re-bind the
2322        // data and discard the view if we fail.
2323        final View transientView = mRecycler.getTransientStateView(position);
2324        if (transientView != null) {
2325            final LayoutParams params = (LayoutParams) transientView.getLayoutParams();
2326
2327            // If the view type hasn't changed, attempt to re-bind the data.
2328            if (params.viewType == mAdapter.getItemViewType(position)) {
2329                final View updatedView = mAdapter.getView(position, transientView, this);
2330
2331                // If we failed to re-bind the data, scrap the obtained view.
2332                if (updatedView != transientView) {
2333                    setItemViewLayoutParams(updatedView, position);
2334                    mRecycler.addScrapView(updatedView, position);
2335                }
2336            }
2337
2338            // Scrap view implies temporary detachment.
2339            isScrap[0] = true;
2340            return transientView;
2341        }
2342
2343        final View scrapView = mRecycler.getScrapView(position);
2344        final View child = mAdapter.getView(position, scrapView, this);
。。。。。
2364        setItemViewLayoutParams(child, position);
。。。。
2376
2377        return child;
2378    }

http://androidxref.com/5.0.0_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#setItemViewLayoutParams

  private void setItemViewLayoutParams(View child, int position) {
2381        final ViewGroup.LayoutParams vlp = child.getLayoutParams();
2382        LayoutParams lp;
2383        if (vlp == null) {
2384            lp = (LayoutParams) generateDefaultLayoutParams();
2385        } else if (!checkLayoutParams(vlp)) {
2386            lp = (LayoutParams) generateLayoutParams(vlp);
2387        } else {
2388            lp = (LayoutParams) vlp;
2389        }
2390
2391        if (mAdapterHasStableIds) {
2392            lp.itemId = mAdapter.getItemId(position);
2393        }
2394        lp.viewType = mAdapter.getItemViewType(position);
2395        child.setLayoutParams(lp);
2396    }

可以看到在5.0版本,参数先校验时,通不过!checkLayoutParams(vlp)) 重新设置了LayoutParams lp。

然后我们看4.4版本的代码 http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#95

 View obtainView(int position, boolean[] isScrap) {
2228        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "obtainView");
2229
2230        isScrap[0] = false;
2231        View scrapView;
2232
2233        scrapView = mRecycler.getTransientStateView(position);
2234        if (scrapView == null) {
2235            scrapView = mRecycler.getScrapView(position);
2236        }
2237
2238        View child;
2239        if (scrapView != null) {
2240            child = mAdapter.getView(position, scrapView, this);
2241
。。。。
2262        } else {
2263            child = mAdapter.getView(position, null, this);
。。。。
2272        }
2273
2274        if (mAdapterHasStableIds) {
2275            final ViewGroup.LayoutParams vlp = child.getLayoutParams();
2276            LayoutParams lp;
2277            if (vlp == null) {
2278                lp = (LayoutParams) generateDefaultLayoutParams();
2279            } else if (!checkLayoutParams(vlp)) {
2280                lp = (LayoutParams) generateLayoutParams(vlp);
2281            } else {
2282                lp = (LayoutParams) vlp;
2283            }
2284            lp.itemId = mAdapter.getItemId(position);
2285            child.setLayoutParams(lp);
2286        }
2287
。。。。
2299        return child;
2300    }

mAdapterHasStableIds 为true时才检验参数(http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#mAdapterHasStableIds))
那么我们的child的getLayoutParams来自于哪儿呢?

5. inflater.inflate

childView = (ViewGroup) inflater.inflate(R.layout.xxx, container, false); 两个版本代码基本一致。
最终调用LayoutInflater的方法
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)

。。。
 final View temp = createViewFromTag(root, name, inflaterContext, attrs);

                    ViewGroup.LayoutParams params = null;

                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

由于我们的布局attachToRoot为false,调用 setLayoutParams方法时,将container的参数被设置给了child View 。所以结论就是container的参数被塞给了child View , 在obtainView的时候因为版本差异导致异化处理, 而在setupchild设置的时候4.x版crash了。根本原因在于parent View的设置不正确。 那么正确姿势就很简单了, 设置正确的parent的就行,那么是谁呢? 当然是child view添加进的listView了。 为什么不设置null,也很简单, null就导致父类的参数没法设置进child View 了。

你可能感兴趣的:(ListView的一个典型crash cannot be cast to android.widget.AbsListView$LayoutParams)