question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[android] Dismissing a modal can sometimes result in a NPE

See original GitHub issue

Is this a bug report?

This is a bug seen sometimes when dismissing modals on android

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
	at com.facebook.react.uimanager.NativeViewHierarchyManager.dropView(NativeViewHierarchyManager.java:531)
	at com.facebook.react.uimanager.NativeViewHierarchyManager.manageChildren(NativeViewHierarchyManager.java:425)
	at com.facebook.react.uimanager.UIViewOperationQueue$ManageChildrenOperation.execute(UIViewOperationQueue.java:179)
	at com.facebook.react.uimanager.UIViewOperationQueue$1.run(UIViewOperationQueue.java:768)
	at com.facebook.react.uimanager.UIViewOperationQueue.flushPendingBatches(UIViewOperationQueue.java:824)
	at com.facebook.react.uimanager.UIViewOperationQueue.access$1600(UIViewOperationQueue.java:48)
	at com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.doFrameGuarded(UIViewOperationQueue.java:870)
	at com.facebook.react.uimanager.GuardedFrameCallback.doFrame(GuardedFrameCallback.java:31)
	at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:136)
	at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:107)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
	at android.view.Choreographer.doCallbacks(Choreographer.java:723)
	at android.view.Choreographer.doFrame(Choreographer.java:655)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
	at android.os.Handler.handleCallback(Handler.java:789)
	at android.os.Handler.dispatchMessage(Handler.java:98)
	at android.os.Looper.loop(Looper.java:164)
	at android.app.ActivityThread.main(ActivityThread.java:6541)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)


Have you read the Contributing Guidelines?

Yes

Environment

react-native: 0.47.2 => 0.47.2 react: 16.0.0-alpha.12 => 16.0.0-alpha.12

Target Platform: Android (seems to only happen on O - i’ve only seen this on pixel devices)

Steps to Reproduce

This is kind of a difficult one to actually reproduce, as it seems to be related to a race condition. I’ve seen this a few times now from instabug.

  1. Show a Modal
  2. Dismiss a modal
  3. see crash (above)

Expected Behavior

App does not crash

Actual Behavior

App Crashes

Proposed cause

It appears that

https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java#L529

is calling

https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java#L124

by removing the hostview from the parent and dismissing the dialog, our view object is being mutated in some way.

I think it would be smart to hold onto view.getId() before calling onDropInstance then use that to clean up the rest of our views. I’m not entirely sure how this works at this level,

Proposed changes

  /**
   * Releases all references to given native View.
   */
  protected synchronized void dropView(View view) {
    UiThreadUtil.assertOnUiThread();
    int viewId = view.getId();
    if (!mRootTags.get(viewId)) {
      // For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
      resolveViewManager(viewId).onDropViewInstance(view);
    }
    ViewManager viewManager = mTagsToViewManagers.get(viewId);
    if (view instanceof ViewGroup && viewManager instanceof ViewGroupManager) {
      ViewGroup viewGroup = (ViewGroup) view;
      ViewGroupManager viewGroupManager = (ViewGroupManager) viewManager;
      for (int i = viewGroupManager.getChildCount(viewGroup) - 1; i >= 0; i--) {
        View child = viewGroupManager.getChildAt(viewGroup, i);
        if (mTagsToViews.get(child.getId()) != null) {
          dropView(child);
        }
      }
      viewGroupManager.removeAllViews(viewGroup);
    }
    mTagsToViews.remove(viewId);
    mTagsToViewManagers.remove(viewId);
  }

I’d be happy to submit this change if this seems like the right way to go.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
prathammehtacommented, Apr 25, 2018

I have the same issue on a note 8 running Oreo

1reaction
chuckwu0commented, Apr 12, 2018

I have the same crash stack !!

Read more comments on GitHub >

github_iconTop Results From Across the Web

DialogFragment - Android Developers
Because dialogs normally are modal, this will still operate as a back stack, since the dialog will capture user input until it is...
Read more >
android - NullPointerException on dismiss DialogFragment
The framework code that calls this method passes in a value retrieved from a WeakReference , which means that dialog will sometimes be...
Read more >
Diff - platform/frameworks/base - Google Git
-59,6 +59,8 @@ core/java/android/accounts/IAccountManagerResponse.aidl ... <p>This system activity will return once Bluetooth has completed turning @@ -251 ...
Read more >
Changelog for Android and FireOS - Braze
Fixed an issue introduced in 4.0.1 where upgrading the SDK could result in server configuration values getting removed until the next session start....
Read more >
Android Changelog - Airship Docs
Updates will only be available through Maven Central going forward. Changes. Fixed IAA full screen reporting events reporting as dismissed instead of button ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found