Calling ViewGroup # addView and ViewGroup # removeView raises a NullPointerException

Inside the method I call addView, and removeViewas follows:

public class EnclosingClass extends FrameLayout {
    ...
    void fooMethod() {
        ...
        viewGroup1.addView(this);
        viewGroup2.removeView(this);
        ...
    }
    ...
}

This works fine in portrait mode (this is called many times without any problems), but as soon as I change the orientation of the screen to landscape, I get a message NullPointerException: (FYI, fooMethodfrom surfaceChanged while changing the orientation):

 18376         AndroidRuntime  E  FATAL EXCEPTION: main
 18376         AndroidRuntime  E  Process: com.google.android.apps.chrome, PID: 18376
 18376         AndroidRuntime  E  java.lang.NullPointerException
 18376         AndroidRuntime  E  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:405)
 18376         AndroidRuntime  E  at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
 18376         AndroidRuntime  E  at android.view.View.layout(View.java:14817)
 18376         AndroidRuntime  E  at android.view.ViewGroup.layout(ViewGroup.java:4631)
 18376         AndroidRuntime  E  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
 18376         AndroidRuntime  E  at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
 18376         AndroidRuntime  E  at android.view.View.layout(View.java:14817)
 18376         AndroidRuntime  E  at android.view.ViewGroup.layout(ViewGroup.java:4631)
 18376         AndroidRuntime  E  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
 18376         AndroidRuntime  E  at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
 18376         AndroidRuntime  E  at android.view.View.layout(View.java:14817)
 18376         AndroidRuntime  E  at android.view.ViewGroup.layout(ViewGroup.java:4631)
 18376         AndroidRuntime  E  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
 18376         AndroidRuntime  E  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
 18376         AndroidRuntime  E  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
 18376         AndroidRuntime  E  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
 18376         AndroidRuntime  E  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
 18376         AndroidRuntime  E  at android.view.Choreographer.doCallbacks(Choreographer.java:574)
 18376         AndroidRuntime  E  at android.view.Choreographer.doFrame(Choreographer.java:544)
 18376         AndroidRuntime  E  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
 18376         AndroidRuntime  E  at android.os.Handler.handleCallback(Handler.java:733)
 18376         AndroidRuntime  E  at android.os.Handler.dispatchMessage(Handler.java:95)
 18376         AndroidRuntime  E  at android.os.Looper.loop(Looper.java:136)
 18376         AndroidRuntime  E  at android.app.ActivityThread.main(ActivityThread.java:5017)
 18376         AndroidRuntime  E  at java.lang.reflect.Method.invokeNative(Native Method)
 18376         AndroidRuntime  E  at java.lang.reflect.Method.invoke(Method.java:515)
 18376         AndroidRuntime  E  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 18376         AndroidRuntime  E  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 18376         AndroidRuntime  E  at dalvik.system.NativeStart.main(Native Method)

To solve this problem, put addViewand removeViewinside a Runnableas follows:

void fooMethod() {
    ...
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            viewGroup1.addView(EnclosingClass.this);
            viewGroup2.removeView(EnclosingClass.this);
    });
    ...
}

, , , , , . ? , ? , .

+4
1
public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else {
        action.run();
    }`enter code here`
}

runOnUiThread UIThread, else branch: action.run(), . api mChildCount, NULL (UI) )

0

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


All Articles