SurfaceView exception in Unity 2022

Hi. Prior to Unity 2022 I would fetch the SurfaceView by extending the UnityPlayerActivity and then using the following Java code:

FrameLayout frameLayout = (FrameLayout) mUnityPlayer.getView();
SurfaceView surfaceView = (SurfaceView) frameLayout.getChildAt(0);

After upgrading to Unity 2022, it’s throwing an exception:

How should I get the SurfaceView in Unity 2022?

I exported the Android project and when searching through it I found out that you can get the surfaceView through the following code:

SurfaceView surfaceView = findViewById(R.id.unitySurfaceView);
1 Like

I’m facing the same issue.
Did you found any solution ?

** @unity_kIW5vB8P_ITvmw **

Thank you very much.

I have no knowledge about android development, and don’t know how to add “unitySurfaceView” into R.id.
Then I watch android development tutorial and do the way you found.
It’s working so nice. Thank you :wink:

Now I post the more detail answer for others facing the same problem.
Step 1: Just add the ids.xml into res/values/
Step 2: Copy the following content and save it.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="unitySurfaceView" />
</resources>

Step 3: Replace this

with this

1 Like

smoething was lost。。。can you Repost the code?

can you re-upload image at Step 3 ?. Thanks so much !