Removing ImageView crashes on some phones

So for some reason, on my Samsung Galaxy S2 (Android version 4.0.4), when I try to remove ImageView from my RelativeLayout, I get NPE. Here is a line of code that runs

mainView.removeView(someImageView) 

However, on my Galaxy Nexus (Android version 4.1.1) it doesn't even hiccup. I posted the NPE below. Is this the wrong way to remove ImageView? I’m mostly made with an image and I don’t need to show it anymore. Any thoughts or ideas would be great.

 FATAL EXCEPTION: main java.lang.NullPointerException at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2488) at android.view.View.draw(View.java:11150) at android.view.ViewGroup.drawChild(ViewGroup.java:2887) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) at android.view.View.draw(View.java:11150) at android.view.ViewGroup.drawChild(ViewGroup.java:2887) at android.view.View.draw(View.java:11150) at android.view.ViewGroup.drawChild(ViewGroup.java:2887) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) at android.view.ViewGroup.drawChild(ViewGroup.java:2885) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) at android.view.ViewGroup.drawChild(ViewGroup.java:2885) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) at android.view.View.draw(View.java:11150) at android.widget.FrameLayout.draw(FrameLayout.java:450) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2302) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2127) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1733) at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2545) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4507) 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:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method) !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error 

EDIT: I don't think this has anything to do with the problem, but it is removed using the "onAnimationEnd" method for translational animation.

+3
source share
1 answer

Try setting the image visibility in GONE as:

 someImageView.setVisibility(View.GONE); 
+5
source

Source: https://habr.com/ru/post/1436821/


All Articles