Android crash on Unity 2019.4.26f

Hi there,
I got crashes on Android 7.0 & 7.1. My Unity version is 2019.4.26f, I’m not sure if it would be fixed on Unity 2019.4.27f or not.
Here’s stacktrace I got from Android Vital on Google Play Console:

Caused by 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:4636)
       at android.view.ViewGroup.addView(ViewGroup.java:4477)
       at android.view.ViewGroup.addView(ViewGroup.java:4417)
       at android.view.ViewGroup.addView(ViewGroup.java:4390)
       at com.unity3d.player.j.a()
       at com.unity3d.player.UnityPlayer$21.surfaceCreated()
       at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
       at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:258)
       at android.view.View.dispatchWindowVisibilityChanged(View.java:11278)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1293)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1293)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1293)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1293)
       at com.android.internal.policy.DecorView.dispatchWindowVisibilityChanged(DecorView.java:454)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1875)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1567)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7282)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:930)
       at android.view.Choreographer.doCallbacks(Choreographer.java:705)
       at android.view.Choreographer.doFrame(Choreographer.java:640)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:916)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6823)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

Same here

Anyone got this bug?

Hi, we have the same problem after upgrading Unity to 2020.

2021-06-01 16:06:13.022 18219-18219/com.blablab E/SurfaceView: Exception configuring surface
    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:5038)
        at android.view.ViewGroup.addView(ViewGroup.java:4869)
        at android.view.ViewGroup.addView(ViewGroup.java:4809)
        at android.view.ViewGroup.addView(ViewGroup.java:4782)
        at com.unity3d.player.f.a(Unknown Source:4)
        at com.unity3d.player.UnityPlayer$19.surfaceCreated(Unknown Source:18)
        at android.view.SurfaceView.updateSurface(SurfaceView.java:675)
        at android.view.SurfaceView$2.onPreDraw(SurfaceView.java:143)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2516)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1500)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7341)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1048)
        at android.view.Choreographer.doCallbacks(Choreographer.java:843)
        at android.view.Choreographer.doFrame(Choreographer.java:775)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1034)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6882)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

I traced it to version 2020.3.4, 2020.3.3 works fine. My guess is that it was cause by this fix (seems like it was merged to 2019.4.26 too):

  • Android: Fixed an issue with the black Unity view that occurred after pausing the app when in-app purchase dialog is shown. (1249179)

It seems like there was some new m_PersistentUnitySurface added to UnityPlayer that listens to
surfaceCreated/surfaceChanged/surfaceDestroyed callbacks from the holder of the SurfaceView returned by
CreateGlView. The new class seems to add a view to the UnityPlayer in the surfaceCreated event.
This works fine the first time but it seems like there are scenarios where the view was already added in a previous surfaceCreated event and that’s when the exception is thrown (since the view it’s trying to add already have a parent). The only place it seems to remove the view is in checkResumePlayer. So it seems like there is either some missing check for if parent is already set, or a that it’s missing to remove the view from UnityPlayer at some point (surfaceDestroyed?)?

We are integrating Unity in a React-native application where we create a UnityPlayer in our existing activity (the same UnityPlayer instance is used through the app). The UnityPlayer is then added/removed from a view when we want to show/hide it. This seems to trigger the surface to be recreated, which then triggers the exception. In our case it doesn’t crash the app, but leads to a black screen which then seems to be fine if you minimize the app and take it back in focus.

I tried to hack around the issue by extending UnityPlayer and overriding onViewAdded like so:

public void onViewAdded(View view) {
super.onViewAdded(view);
if(view.getClass() != SurfaceView.class) {
this.removeView(view);
}
}

This seems to work for out case, but really hacky way to get around the issue and I’m not sure if it can have any other consequences (apart from removing splash screen too).

What’s the use of the m_PersistentUnitySurface anyway, any other ideas for how to get around this? Or can this be fixed in a future release? Seems like there are more people that has this issue.

3 Likes

The same problem after migrate to Unity 2020.3 lts

Help, please!

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

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:4419)
    at android.view.ViewGroup.addView(ViewGroup.java:4260)
    at android.view.ViewGroup.addView(ViewGroup.java:4200)
    at android.view.ViewGroup.addView(ViewGroup.java:4173)
    at com.unity3d.player.j.a(Unknown Source)
    at com.unity3d.player.UnityPlayer$21.surfaceCreated(Unknown Source)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:634)
    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:251)
    at android.view.View.dispatchWindowVisibilityChanged(View.java:10375)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1585)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1295)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6400)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
    at android.view.Choreographer.doCallbacks(Choreographer.java:688)
    at android.view.Choreographer.doFrame(Choreographer.java:623)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:862)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6396)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

Can anyone please report a bug with a project to reproduce the issue for this?

Dont have device with android 7. =(
By our logs looks like player saw Ads and after has crash.

Same problem on Unity 2020.3.9f1

Reports from Firebase Crashlytics: Android 7 - 100%

Caused by 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:4656)
       at android.view.ViewGroup.addView(ViewGroup.java:4497)
       at android.view.ViewGroup.addView(ViewGroup.java:4437)
       at android.view.ViewGroup.addView(ViewGroup.java:4410)
       at com.unity3d.player.f.a()
       at com.unity3d.player.UnityPlayer$19.surfaceCreated()
       at android.view.SurfaceView.updateWindow(SurfaceView.java:656)
       at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:266)
       at android.view.View.dispatchWindowVisibilityChanged(View.java:11077)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1290)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1515)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7091)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
       at android.view.Choreographer.doCallbacks(Choreographer.java:702)
       at android.view.Choreographer.doFrame(Choreographer.java:638)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6682)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Plugins used in projects: Firebase, Admob, Facebook

1 Like

Unity Ads or something else?

We use mediation admob, facebook, unity ads. Don’t know who exact.

Have the same problem after migrating to 2019.4.26 from 2018.4.
Any update on this issue?

We are still looking for a bug report to reproduce the issue.
I had a look at the Android’s SurfaceView implementations on Android 7.x and 8.x and there were some changes that could cause this.

New
Same problem on Unity 2019.4.28f1

Reports from Firebase Crashlytics: Android 7 - 100% Recent days so many crashes happened

using IronSource Mediation - admob,adcolony,facebook and unity

2 Likes

Similarly, sadly we couldn’t reproduce it on our end.

In another thread, I posted some info about a similar problem. Maybe this will give some hint to the support. There is about the destruction of the surface

Same Issue here with Unity 2020.3.12f1, also 100% from Android 7 in Firebase Crashlytics.

1 Like

Same issue with Unity 2019.4.28.
Issue 1: The specified child already has a parent. You must call removeView() on the child’s parent first.
Issue 2: com.unity3d.player.UnityPlayer$21.surfaceDestroyed

Issues appeared after migration to this version of unity from 2019.4.19.
Also have various mediation networks in my project and don’t know the cause of the crashes.

2 Likes

We have the same issue using Unity 2021.1.7f1, unity ads, admob and mediation.
Have you found any workaround?
This is our error:

java.lang.Error: FATAL EXCEPTION [main]
Unity version : 2021.1.7f1
Device model : samsung SM-G570F
Device fingerprint: samsung/on5xeltejv/on5xelte:7.0/NRD90M/G570FXXU1BQL2:user/release-keys
Build Type : Release
Scripting Backend : IL2CPP
ABI : armeabi-v7a
Strip Engine Code : true
Caused by
at android.view.ViewGroup.addViewInner (ViewGroup.java:4658)
at android.view.ViewGroup.addView (ViewGroup.java:4499)
at android.view.ViewGroup.addView (ViewGroup.java:4439)
at android.view.ViewGroup.addView (ViewGroup.java:4412)
at com.unity3d.player.f.a (Unknown Source)
at com.unity3d.player.UnityPlayer$19.surfaceCreated (Unknown Source)
at android.view.SurfaceView.updateWindow (SurfaceView.java:656)
at android.view.SurfaceView.onWindowVisibilityChanged (SurfaceView.java:266)
at android.view.View.dispatchWindowVisibilityChanged (View.java:11139)
at android.view.ViewGroup.dispatchWindowVisibilityChanged (ViewGroup.java:1290)
at android.view.ViewGroup.dispatchWindowVisibilityChanged (ViewGroup.java:1290)
at android.view.ViewGroup.dispatchWindowVisibilityChanged (ViewGroup.java:1290)
at android.view.ViewGroup.dispatchWindowVisibilityChanged (ViewGroup.java:1290)
at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1842)
at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1537)
at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run (Choreographer.java:959)
at android.view.Choreographer.doCallbacks (Choreographer.java:734)
at android.view.Choreographer.doFrame (Choreographer.java:670)
at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:945)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6776)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)

After upgrading from Unity 2018 to 2019.4.26 we can see a lot of these errors reported for Android 6, 5,1, 5.

Type
java.lang.Error

Show less
expand_less
java.lang.Error: FATAL EXCEPTION [main]
Unity version     : 2019.4.26f1
Device model      : LGE LG-K220
Device fingerprint: lge/mk6p_global_com/mk6p:6.0.1/MXB48T/1904311239303:user/release-keys
Caused by
  at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:3924)
  at android.app.ActivityThread.handleDestroyActivity (ActivityThread.java:3942)
  at android.app.ActivityThread.access$1500 (ActivityThread.java:163)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1416)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:148)
  at android.app.ActivityThread.main (ActivityThread.java:5585)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)
Caused by: java.lang.NullPointerException:
  at com.unity3d.player.UnityPlayer$21.surfaceDestroyed (Unknown Source)
  at android.view.SurfaceView.updateWindow (SurfaceView.java:581)
  at android.view.SurfaceView.onWindowVisibilityChanged (SurfaceView.java:243)
  at android.view.View.dispatchDetachedFromWindow (View.java:14633)
  at android.view.ViewGroup.removeAllViewsInLayout (ViewGroup.java:4834)
  at android.view.ViewGroup.removeAllViews (ViewGroup.java:4780)
  at com.unity3d.player.UnityPlayer.destroy (Unknown Source)
  at com.unity3d.player.UnityPlayerActivity.onDestroy (UnityPlayerActivity.java:69)
  at android.app.Activity.performDestroy (Activity.java:6449)
  at android.app.Instrumentation.callActivityOnDestroy (Instrumentation.java:1143)
  at android.app.ActivityThread.performDestroyActivity (ActivityThread.java:3911)

We see this happening for IronSource mediation with Admob, FB and Unity as well as Admob mediation with just Fb (so at least I think it’s not the Unity Ads).
I don’t know how to reproduce this.

I reported this here: Unity Issue Tracker - [Android] Crash with java.lang.IllegalStateException: The specified child already has a parent.

We think we have a fix but since we don‘t have a way to reproduce the problem we are only 80% sure.
Since the fix is considered low-risk we are back-porting the change to all relevant versions of Unity.

1 Like