2017-01-24 79 views
3

我已經實現了一個動畫,其中我在imageview中的回收視圖中,點擊該項目時我想對圖像執行一些動畫鑑於回收視野之外,使 - 我創建了一個新的ImageView, - 把它添加到主容器 - 做了它的動畫 - 最後我從主容器獲取NullPointerException:嘗試從字段'int android.view.View.mViewFlags'中讀取 - Android動畫

這裏刪除它的代碼回收查看點擊項目:

代碼:

final ImageView iv = new ImageView(getContext()); 
iv.setImageDrawable(fromView.getDrawable()); 
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
iv.setLayoutParams(fromView.getLayoutParams()); 
v_mainContainer.addView(iv); 

AnimationSet animatinSet = new AnimationSet(false); 
animatinSet.setFillAfter(false); 
animatinSet.setFillBefore(true); 
animatinSet.setDuration(1500); 
animatinSet.setInterpolator(new DecelerateInterpolator()); 

// ... rest of the animation 

animatinSet.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     v_mainContainer.removeView(iv); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}); 

animatinSet.setAnimationListener(animationListner); 
animatinSet.setDuration(1500); 

iv.startAnimation(animatinSet); 

當我在1視圖上運行此動畫時,它工作正常。不過,當我在相同的視圖上多次運行動畫時,以及在我運行另一個視圖之前完成動畫時,我會看到下面的錯誤。

錯誤日誌:

E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.mbh.testApp, PID: 9628 
    java.lang.NullPointerException: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference 
     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3549) 
     at android.view.View.draw(View.java:17071) 
     at android.view.View.updateDisplayListIfDirty(View.java:16050) 
     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) 
     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) 
     at android.view.View.updateDisplayListIfDirty(View.java:16013) 
     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) 
     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) 
     at android.view.View.updateDisplayListIfDirty(View.java:16013) 
     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) 
     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) 
     at android.view.View.updateDisplayListIfDirty(View.java:16013) 
     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) 
     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) 
     at android.view.View.updateDisplayListIfDirty(View.java:16013) 
     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3748) 
     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3728) 
     at android.view.View.updateDisplayListIfDirty(View.java:16013) 
     at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656) 
     at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662) 
     at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770) 
     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796) 
     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604) 
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211) 
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) 
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301) 
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) 
     at android.view.Choreographer.doCallbacks(Choreographer.java:683) 
     at android.view.Choreographer.doFrame(Choreographer.java:619) 
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) 
     at android.os.Handler.handleCallback(Handler.java:751) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6077) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我試圖把從主容器取出視圖時try-catch塊,但它並沒有改變結果,並仍然得到同樣的錯誤。

我也試過這個答案https://stackoverflow.com/a/4295570/2296787並沒有解決問題

奇怪的是,該錯誤犯規給我看完全異常升高。

非常感謝您的幫助。

+1

在模擬器它我沒有工作。運行在真實的設備上,然後它工作正常 – sasikumar

+0

@sasikumar正確!它在真實設備上運行良好!它被固定在仿真器上,當我使用postDelayed以500毫秒的延遲去除視圖! 非常感謝你 – MBH

+0

快樂編碼! – sasikumar

回答

2

由於@sasikumar提到的解決方案是真正設備

上運行測試,但也解決了我在模擬器上還加入postDelayed在動畫結束監聽器移除視圖前500毫秒的延遲:

animatinSet.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     iv.setVisibility(View.GONE); 
     v_mainContainer.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       v_mainContainer.removeView(iv); 
      } 
     }, 500); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
    } 
}); 

這同時適用於模擬器和真實設備

1

在仿真器中它不起作用。上然後它是工作的罰款真實設備運行..

相關問題