Removing ImageView causes crash on certain phones

2019-05-11 00:01发布

问题:

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

mainView.removeView(someImageView)

However on my Galaxy Nexus (Android version 4.1.1) it does not even hiccup. I have posted the NPE below. Is this not the proper way to remove an ImageView? basically I am done with the image and I don't need it showing any more. 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 it has anything to do with the problem but this is being removed on the "onAnimationEnd" method of a translate animation.

回答1:

Try to set visibility of the imageView to GONE like:

someImageView.setVisibility(View.GONE);