动画结束时的Android java.lang.NullPointerException

Posted

技术标签:

【中文标题】动画结束时的Android java.lang.NullPointerException【英文标题】:Android java.lang.NullPointerException at animation end 【发布时间】:2015-08-21 06:12:27 【问题描述】:

在动画结束时删除视图时我面临异常。只有在播放动画的同时将应用程序置于前台时才会发生这种情况。下面是代码sn-p。任何帮助将不胜感激。

public void onAnimationEnd(Animation animation) 
                                new Handler().post(new Runnable() 
                                    @Override
                                    public void run() 
                                        try
                                            setActivePlayer(nextPlayer, time);
                                            String source = discardAnimationCard.getCardSource();
                                            openCard.updateCardSource(source);
                                            if(gameTable.indexOfChild(discardAnimationCard) >= 0)
                                                gameTable.removeView(discardAnimationCard);
                                            
                                            String discardAckMessage = "DiscA#";
                                            String round = "1";
                                            if( gameId.indexOf("-") >= 0 )
                                                round = gameId.split("-")[1];
                                            
                                            discardAckMessage += gameCode+"@"+round+":"+source;
                                            gameDataReader.gameHandler.sendMessage(discardAckMessage);
                                        catch(Exception e)

                                        

                                    
                                );

下面是堆栈跟踪

java.lang.NullPointerException
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.draw(View.java:14509)
            at android.view.View.getDisplayList(View.java:13403)
            at android.view.View.getDisplayList(View.java:13445)
            at android.view.View.draw(View.java:14223)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
            at android.view.View.draw(View.java:14509)
            at android.widget.FrameLayout.draw(FrameLayout.java:472)
            at android.view.View.getDisplayList(View.java:13403)
            at android.view.View.getDisplayList(View.java:13445)
            at android.view.View.draw(View.java:14223)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
            at android.view.View.getDisplayList(View.java:13398)
            at android.view.View.getDisplayList(View.java:13445)
            at android.view.View.draw(View.java:14223)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
            at android.view.View.draw(View.java:14509)
            at android.widget.FrameLayout.draw(FrameLayout.java:472)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2452)
            at android.view.View.getDisplayList(View.java:13403)
            at android.view.View.getDisplayList(View.java:13445)
            at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
            at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2431)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2301)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5105)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

请点击edit并添加stacktrace作为LogCat的输出。我们无法调试我们看不到的东西。由于缺乏背景,投票结束。 以上堆栈跟踪取自 LogCat。 【参考方案1】:

当你在 animationEnd 中改变视图层次时,Android 会抛出异常。 检查此链接它将解决您的问题 NullPointerException that doesn't point to any line in my code

【讨论】:

在上面的代码中我使用了 Handler post。但是还是会出现异常 那个解决方案对我不起作用。还要别的吗。我仍然面临这个问题... ??

以上是关于动画结束时的Android java.lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

android循环属性动画结束后状态怎样变回原状态

获取元素的结束位置。动画结束时的位置。在 OOP Javascript/jQuery 中

android 动画

android的三种动画

Android--activity切换时的动画

如何设定Android Activity间切换时的动画