前言
? ? ? 我在做收到禮物 顯示 動畫,并在指定時間內(nèi)自動劃走的需求中,莫名其妙的遇到了如下異常,并沒有定位到具體哪行出了問題,于是排除業(yè)務(wù)邏輯,查源碼定位到問題所在。

問題代碼:
? ? 在動畫結(jié)束回調(diào)onAnimationEnd()中remove view觸發(fā)invalidate(),然后再dispatchDraw方法中的child.mViewFlag獲取中拋出NullPointerException,繼續(xù)尋找,函數(shù)getAndVerifyPreorderedView 來獲取child,具體的參數(shù)情況,是 children 里的個數(shù)是1,但是childIndex是1,得到的結(jié)果肯定null。

@Override
? ? protected void dispatchDraw(Canvas canvas) {
? ? ? ? ...省略若干代碼.....
? ? ? ? final int childrenCount = mChildrenCount;
? ? ? ? final View[] children = mChildren;
? ? ? ...省略若干代碼.....
? ? ? ? // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
? ? ? ? // draw reordering internally
? ? ? ? final ArrayList<View> preorderedList = usingRenderNodeProperties
? ? ? ? ? ? ? ? ? null : buildOrderedChildList();
? ? ? ? final boolean customOrder = preorderedList == null
? ? ? ? ? ? ? ? && isChildrenDrawingOrderEnabled();
? ? ? ? for (int i = 0; i < childrenCount; i++) {
? ? ? ? ? ? while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
? ? ? ? ? ? ? ? final View transientChild = mTransientViews.get(transientIndex);
? ? ? ? ? ? ? ? if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
? ? ? ? ? ? ? ? ? ? ? ? transientChild.getAnimation() != null) {
? ? ? ? ? ? ? ? ? ? more |= drawChild(canvas, transientChild, drawingTime);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? transientIndex++;
? ? ? ? ? ? ? ? if (transientIndex >= transientCount) {
? ? ? ? ? ? ? ? ? ? transientIndex = -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
? ? ? ? ? ? final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
? ? ? ? ? ? //這里發(fā)生了空指針異常,child為null
? ? ? ? ? ? if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
? ? ? ? ? ? ? ? more |= drawChild(canvas, child, drawingTime);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ...省略若干代碼.....
? }? ?
解決辦法:
知道了原因,再來解決就很簡單了。以最開始的核心問題代碼,來演示如何解決。問題出現(xiàn)remove view的時候,在dispatchDraw 中改變了viewGroup已有的子view的數(shù)量,導(dǎo)致只有N個view,最大索引是N-1,想要獲取第N個view,出現(xiàn)了異常。那么我們可以考慮不在本次執(zhí)行中,remove view。在下一次的loop消息中執(zhí)行remove 操作,那么就通過post 或 handler 發(fā)送消息來操作view。
