Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when adding/removing views of a RelativeLayout (Animations)

Hi I want to create a Image and animate from bottom to top and change the alpha from 1.0 to 0.0. I have this code (I call 3 times in onCreate method to make 3 animated images):

/**
 * Starts to make fog
 */
private void startFogGenerator() {

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            final ImageView img = new ImageView(Activity.this);

            boolean showN1 = r.nextBoolean();
            if (showN1) {
                img.setImageResource(R.drawable.nube_01);
            } else {
                img.setImageResource(R.drawable.nube_02);
            }

            Animation animation = AnimationUtils.loadAnimation(Activity.this, R.anim.translate_and_alpha);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    fogLayout.removeView(img);
                    fogLayout.invalidate();
                    // always true? Maybe but just check for for concurrency safe.
                    if (setFogN(false) < MAX_CLOUDS_ON_SCREEN) {
                        startFogGenerator();
                    }

                }

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


            int pxWidth = Tools.toPx(140, getResources()); // Width of layout container
            int paddingWidth = Tools.toPx(20, getResources());
            int x = r.nextInt(pxWidth - paddingWidth - paddingWidth) + paddingWidth;

            int range = Tools.toPx(50, getResources());

            int width;
            int height;
            if (showN1) { // nube01: 167x226
                width = r.nextInt(Tools.toPx(167, getResources()) - range) + range;
                height= r.nextInt(Tools.toPx(226, getResources()) - range) + range;
            } else { // nube01: 144x177
                width = r.nextInt(Tools.toPx(144, getResources()) - range) + range;
                height= r.nextInt(Tools.toPx(177, getResources()) - range) + range;
            }

            Log.d("Animation", "X: " + x + ", Width: " + width + ", Height: " + height);

            //img.setPadding(x, 0, 0, 0);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
            params.leftMargin = x;
            fogLayout.addView(img, params);
            img.setBackgroundColor(Color.argb(255, r.nextInt(255),r.nextInt(255),r.nextInt(255))); // just for debug
            img.startAnimation(animation);
            setFogN(true);

        }
    }, r.nextInt(2500 - 200) + 200);

}
private final static int MAX_CLOUDS_ON_SCREEN = 3;
private AtomicInteger currentFogs = new AtomicInteger(0);
private synchronized int setFogN(boolean increment) {

    int toReturn = increment ? currentFogs.incrementAndGet() : currentFogs.decrementAndGet();

    Log.d("TeaAnim", "ToReturn: "+ toReturn);

    return toReturn;

}

Works ok but when removes the 3th view and starts another time, it crashes with NullPointerException, maybe is problem of concurrency? How I can fix it?

Exception:

ERROR/AndroidRuntime(10736): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2796)
    at android.view.View.getDisplayList(View.java:12648)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910)
    at android.view.View.getDisplayList(View.java:12588)
    at android.view.View.getDisplayList(View.java:12694)
    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5204)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    at dalvik.system.NativeStart.main(Native Method)

Notes: fogLayout is a RelativeLayout with a fixed width and height (in dp) Tools.toPx is a method that coverts from DP to PX

like image 857
PaNaVTEC Avatar asked Mar 26 '13 12:03

PaNaVTEC


1 Answers

Solved, just inform that the handler i am Removing the view with:

handler.post(new Runnable() {
    public void run() {
        fogLayout.removeView(img);
    }
});
like image 85
PaNaVTEC Avatar answered Sep 21 '22 06:09

PaNaVTEC