NullPointerException that doesn't point to any line in my code

后端 未结 5 1962
無奈伤痛
無奈伤痛 2020-12-04 19:22

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 i

相关标签:
5条回答
  • 2020-12-04 19:41

    Can you try this, to ensure that simulateMovingImg is present

        if (myLayout.indexOfChild() >= 0)
        {
            myLayout.removeView(simulateMovingImg);
        }
    
    0 讨论(0)
  • 2020-12-04 19:41

    I met this issue after I updated the appcompat-v7. The latest stable version of the above-mentioned dependency to now is:

      compile 'com.android.support:appcompat-v7:23.2.1'
    
    0 讨论(0)
  • 2020-12-04 19:44

    I slightly changed an accepted answer (it also works) to avoid handlers.

    @Override
    public void onAnimationEnd(Animation animation) {
        imageView.post(new Runnable() {
            @Override
            public void run() {
                layout.removeView(imageView);
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-04 20:06

    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);
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-04 20:06

    My guess is that it is a multi threading problem. It looks like the internal android render loop is encountering a null pointer due to the fact that you removed the image from another thread. The Android UI framework is single threaded and all modifications to the UI must happen in the UI thread. If this is the problem you are encountering then you can just delegate your call to the UI thread.

    There is some related information here: android threading

    0 讨论(0)
提交回复
热议问题