Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash fix for Parent already exist issue #2977

Merged
merged 1 commit into from
Oct 29, 2020

Conversation

ghost
Copy link

@ghost ghost commented Oct 28, 2020

Summary

Check for parent already exists before adding the CardLayout to parent

Motivation

I have integrated the Stripe UI in react native application using the bridge and came across this error after upgrading to 16.0.1 from 15.1.0. As I notice, this layout change has been done in 16.0.0 which caused the crash.

2020-10-28 15:18:48.081 14287-14287/com.project.demo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.project.demo, PID: 14287
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5235)
        at android.view.ViewGroup.addView(ViewGroup.java:5064)
        at android.view.ViewGroup.addView(ViewGroup.java:5004)
        at android.view.ViewGroup.addView(ViewGroup.java:4976)
        at com.stripe.android.view.CardNumberTextInputLayout.onAttachedToWindow(CardNumberTextInputLayout.kt:37)
        at android.view.View.dispatchAttachedToWindow(View.java:20479)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5278)
        at android.view.ViewGroup.addView(ViewGroup.java:5064)
        at android.view.ViewGroup.addView(ViewGroup.java:5004)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1459)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2019)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1965)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1861)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Testing

Replicated the crash process with locally generated AAR file

@CLAassistant
Copy link

CLAassistant commented Oct 28, 2020

CLA assistant check
All committers have signed the CLA.

@mshafrir-stripe
Copy link
Collaborator

@Mohammedatif thanks for filing this PR. Does this consistently happen in React Native?

@ghost
Copy link
Author

ghost commented Oct 28, 2020

@mshafrir-stripe Yes it is consistent since 16.0.0 update. Basically on every re render of the screen, since same component (Layout) is reused in Native it throws Parent already exists exception.

Comment on lines 38 to 40
if (progressView.parent != null) {
(progressView.parent as ViewGroup).removeView(progressView)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify to

(progressView.parent as? ViewGroup)?.removeView(progressView)

@mshafrir-stripe mshafrir-stripe assigned ghost Oct 28, 2020
@mshafrir-stripe mshafrir-stripe merged commit fae0d09 into stripe:master Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants