NullPointerException异常,不指向任何一行在我的code我的、异常、NullPointerException、code

2023-09-12 23:03:51 作者:妳6毛我6毛咱倆一塊二

我工作的一个游戏,玩家可以拖放在屏幕上的东西。我有一个私人的方法,让我模拟拖/ drop事件对于任何玩家可以走动的物品。用于拖动我实际上离开认为,它们触及的地方是,创造一个新的ImageView与和从被触摸的视图被拉伸设置为drawingCache,然后移动这个新创建以下他们的手指在屏幕上ImageView的。当您松开手指(除去视图)我打电话 myLayout.remove(movingImg); 来得到它关闭屏幕。我有一个问题,如果我开始模拟拖动事件,然后手动拿起其他项目我得到一个空指针上myLayout.remove()调用中的一个,这里是从日志跟踪:

I am working on a game where the player can drag and drop things around the screen. I've got a private method which allows me to simulate a drag/drop event for any of the items that the player can move around. For the dragging I am actually leaving the view that they touched where it is and creating a new ImageView with and setting the drawable to the drawingCache from the touched view, then moving this newly created ImageView around the screen following their finger. When you release your finger (drop the view) I am calling myLayout.remove(movingImg); to get it off of the screen. I am having an issue where if I start a simulated drag event and then manually pick up one of the other items I am getting a null pointer on the myLayout.remove() call, here is the trace from Log:

04-06 10:37:43.610: ERROR/AndroidRuntime(23203): java.lang.NullPointerException
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2122)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.drawChild(ViewGroup.java:2506)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.drawChild(ViewGroup.java:2506)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.drawChild(ViewGroup.java:2506)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.drawChild(ViewGroup.java:2506)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.View.draw(View.java:9032)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.widget.FrameLayout.draw(FrameLayout.java:419)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1910)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewRoot.draw(ViewRoot.java:1608)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1329)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1944)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.os.Looper.loop(Looper.java:126)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at android.app.ActivityThread.main(ActivityThread.java:3997)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at java.lang.reflect.Method.invoke(Method.java:491)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-06 10:37:43.610: ERROR/AndroidRuntime(23203):     at dalvik.system.NativeStart.main(Native Method)

跟踪并不在我的活动指向任何地方。该异常被抛出每当它试图调用myLayout.remove()上的模拟拖动视图。我围绕着这条线有一个try / catch但是,这并没有什么。我知道,这是该行多数民众赞成在给我找麻烦,因为如果我把它注释掉话,我没有得到一个例外,但很明显,那么我​​认为永远不会从屏幕上消失。我建立在摩托罗拉Xoom这个程序,我不知道这是否是一个设备特定问题或不是,但。有谁知道什么可能会在这里吗?

The trace doesn't point to anywhere inside my activity. The exception gets thrown whenever the it tries to call myLayout.remove() on the simulated dragging view. I surrounded this line with a try / catch but that did nothing. I know that this is the line thats giving me trouble because if I comment it out then I don't get an exception but obviously then my view never gets removed from the screen. I am building this app on a Motorola xoom, I am not sure if this is a device specific issue or not though. Does anyone know what might be going on here?

这是从哪里.removeView()获取调用:

This is the where the .removeView() is getting called from:

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            Log.i(myTag, "Animation End");

            try {
                //myLayout.removeView(simulateMovingImg); //This is the line that is throwing a null pointer
                simulateMovingImg.setVisibility(View.GONE); //This is how I am currently getting around the issue
                params = new LayoutParams(oneImg.getWidth(),oneImg.getHeight()); // While its moving it appears larger than normal
                 simulateMovingImg.setLayoutParams(params);                     //  so I set it back to the normal size
                 if(whichTarget == true){
                     targetImg.setImageDrawable(simulateMovingImg.getDrawable()); //targetImg is one of the 'drop zones' so I set its 
                                                                                //  drawable to make it seem like the view was 'dropped' into this zone
                 }else{
                     target1Img.setImageDrawable(simulateMovingImg.getDrawable()); // same with target1Img. 
                 }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             iAmDone = true;
        }
    });

这是一个AnimationListener的翻译动画就是从那里开始移动我的ImageView到它是越来越下降里面

That is inside an AnimationListener for the translate animation that is moving my ImageView from where it starts to where it is getting 'dropped'

推荐答案

在更改视图层次中animationEnd Android将是一个例外。

Android will make an exception when you change the view hierarchy in animationEnd.

所有你需要做的就是推迟你的电话是这样的:

All you have to do is to postpone your call like this :

@Override
public void onAnimationEnd(Animation animation) {
    new Handler().post(new Runnable() {
        public void run() {
            myLayout.removeView(simulateMovingImg);
        }
    });
}