Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference

前言

? ? ? 我在做收到禮物 顯示 動畫,并在指定時間內(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。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容