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

Fatal Exception: java.lang.IllegalStateException #149

Closed
Canato opened this issue Jan 27, 2021 · 13 comments
Closed

Fatal Exception: java.lang.IllegalStateException #149

Canato opened this issue Jan 27, 2021 · 13 comments
Assignees
Labels
Released Released already on the latest version.

Comments

@Canato
Copy link

Canato commented Jan 27, 2021

Please complete the following information:

  • Library Version 1.2.9
  • Affected Device(s) Samsung, Xiaomi, HUAWEI, OPPO OS 8,9,10

The Bug:

I'm using only showAlignTop method, but in some cases this crash is popping on my Firebase Crashlytics.

The specified child already has a parent. You must call removeView() on the child's parent first.

Fatal Exception: 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:5106)
       at android.view.ViewGroup.addView(ViewGroup.java:4935)
       at android.view.ViewGroup.addView(ViewGroup.java:4907)
       at android.widget.PopupWindow.createBackgroundView(PopupWindow.java:1530)
       at android.widget.PopupWindow.preparePopup(PopupWindow.java:1492)
       at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1417)
       at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1380)
       at com.skydoves.balloon.Balloon$showAlignTop$$inlined$show$1.run(Balloon.java:1774)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7403)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

Possible solution:

Was not able to full reproduce yet, but maybe we can find the right part of the library and to some parent check, like

if(tv.getParent() != null) {
    ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
layout.addView(tv);
@skydoves
Copy link
Owner

Hi, @Canato!
Do you use the setLayout method for customizing the balloon popup?

@Canato
Copy link
Author

Canato commented Jan 27, 2021

Hi, @Canato!
Do you use the setLayout method for customising the balloon popup?

Thanks for the fats answer. I didn't

This is my creation code

private fun newBalloon(text: String, clickListener: (() -> Unit)? = null): Balloon? {
        latestBalloon?.dismiss()
        latestBalloon = null
        val listener = if (clickListener == null) {
            { }
        } else {
            { clickListener() }
        }

        return context?.let { ctx ->
            createBalloon(ctx) {
                setText(text)
                setOnBalloonClickListener(OnBalloonClickListener { listener() })

                setArrowSize(12)
                setDismissWhenClicked(true)
                setDismissWhenTouchOutside(false)
                setArrowOrientation(ArrowOrientation.BOTTOM)
                setArrowConstraints(ArrowConstraints.ALIGN_ANCHOR)
                setArrowPosition(0.5f)
                setTextSize(18f)
                setCornerRadius(24f)
                setPadding(12)
                setElevation(8)
                setMarginBottom(12)
                isRtlSupport(resources.getBoolean(R.bool.is_right_to_left))
                setTextTypeface(Typeface.BOLD)
                ResourcesCompat.getFont(ctx, R.font.spezia_bold)?.let { setTextTypeface(it) }
                setTextColor(
                    ContextCompat.getColor(
                        ctx,
                        R.color.muzmatch_3_0_light_type_headers_color
                    )
                )
                setTextGravity(Gravity.CENTER)
                setTextIsHtml(true)
                setBackgroundColor(ContextCompat.getColor(ctx, R.color.muzmatch_white_color))
                setBalloonAnimation(BalloonAnimation.FADE)
                setLifecycleOwner(lifecycleOwner)
            }
        }
    }

In my case I have 3 tooltips in the screen, some time I need to hide all (dismiss) and show again later, anchor again on the same view.

@Canato
Copy link
Author

Canato commented Jan 27, 2021

Making some more search, when we dismiss we remove the parent only on the chain of calls:
fun dismiss() -> this.bodyWindow.dismiss() -> onTransitionEnd -> dismissImmediate(decorView, contentHolder, contentView) -> mWindowManager.removeViewImmediate(decorView);

this could means that we can try to setup the parent again because isShowing flag is already false

A small concurrent issue.

Would be sensible to create a showOnLastAnchor() method to use after dismiss() ?

@skydoves skydoves self-assigned this Jan 28, 2021
@skydoves skydoves added the Release Next This feature will be released on next version label Jan 28, 2021
@skydoves
Copy link
Owner

Thanks for your trace!
This issue will be resolved in the next release.

In the PopupWindow, the contentView should be added on the background view of the PopupWindow internally and that was the reason for creating the concurrency issue.

So I just removed the unused setBackgroundDrawable. (d7d1b1d)

if (mBackground != null) {
            mBackgroundView = createBackgroundView(mContentView);
            mBackgroundView.setBackground(mBackground);
        } else {
            mBackgroundView = mContentView;
        }

@skydoves
Copy link
Owner

@Canato
Released a new version 1.3.1!
Please check about this issue :)

@Canato
Copy link
Author

Canato commented Jan 28, 2021

Thanks @skydoves putting for test soon, working as expecte will close this

@Canato
Copy link
Author

Canato commented Jan 29, 2021

Look like this is fixed @skydoves found other issue, but my code I believe. Thanks, let you know anything

@skydoves
Copy link
Owner

@Canato
Thanks for your issue and supports! 👍

@skydoves skydoves added Released Released already on the latest version. and removed Release Next This feature will be released on next version labels Jan 29, 2021
@Canato
Copy link
Author

Canato commented Feb 1, 2021

@skydoves over the weekend, pop up a similar issue in another place

Fatal Exception: 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:5958)
       at android.view.ViewGroup.addView(ViewGroup.java:5777)
       at android.view.ViewGroup.addView(ViewGroup.java:5734)
       at android.widget.PopupWindow.createDecorView(PopupWindow.java:1595)
       at android.widget.PopupWindow.preparePopup(PopupWindow.java:1535)
       at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1449)
       at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1412)
       at com.skydoves.balloon.Balloon$showAlignTop$$inlined$show$1.run(Balloon.java:2092)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7948)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

But instead of createBackgroundView now is createDecorView

Want me to open a new issue?

@skydoves
Copy link
Owner

skydoves commented Feb 1, 2021

@Canato
Hmm.. I think the balloon should be shown when the contentView has no parent view for preventing the concurrency issue.
I will test and add some more conditions on the show method.
Thanks for your report :)

@skydoves skydoves reopened this Feb 1, 2021
@skydoves
Copy link
Owner

skydoves commented Feb 1, 2021

A new alpha version 1.3.2-alpha01 has been released.
The showing conditions have been modified based on your issue.

@Canato
Copy link
Author

Canato commented Feb 2, 2021

100% safe @skydoves thanks for the fast responsive on this. muzmatch working like an charm

@skydoves skydoves closed this as completed Feb 2, 2021
@skydoves
Copy link
Owner

skydoves commented Feb 2, 2021

@Canato
Perfect! Thanks for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released Released already on the latest version.
Projects
None yet
Development

No branches or pull requests

2 participants