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)
....
2. 為什麼會出現crash
測試的時候,發現5.x不會crash,4.x必然重現是什麼原因呢?
我們發現棧頂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
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 }
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。
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 }
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 了。