'App Crash When Scrolling RecyclerView with SurfaceView Items in Galaxy fold3 (Android 12)

I'm having an issue where my app suddenly crashes when scrolling in recycler view with a list of surface views created by RtcEngine.CreateRendererView(getApplicationContext()). These are the logs I get in logcat.

2022-02-11 04:31:51.236 7849-7849/com.myapp.app E/VideoBoxAdapter: onBindViewHolder(): Layout parameters cannot be null
2022-02-11 04:31:51.485 7849-7849/com.myapp.app E/VideoBoxAdapter: onBindViewHolder(): Layout parameters cannot be null
2022-02-11 04:31:51.496 7849-8051/com.myapp.app E/EGL_emulation: eglQueryContext 32c0  EGL_BAD_ATTRIBUTE
2022-02-11 04:31:51.497 7849-8051/com.myapp.app E/EGL_emulation: tid 8051: eglQueryContext(2160): error 0x3004 (EGL_BAD_ATTRIBUTE)
2022-02-11 04:31:52.702 7849-7849/com.myapp.app E/VideoBoxAdapter: onBindViewHolder(): Layout parameters cannot be null
2022-02-11 04:31:52.757 7849-8058/com.myapp.app E/EGL_emulation: eglQueryContext 32c0  EGL_BAD_ATTRIBUTE
2022-02-11 04:31:52.757 7849-8058/com.myapp.app E/EGL_emulation: tid 8058: eglQueryContext(2160): error 0x3004 (EGL_BAD_ATTRIBUTE)
2022-02-11 04:31:58.579 7849-8120/com.myapp.app E/EGL_emulation: eglQueryContext 32c0  EGL_BAD_ATTRIBUTE
2022-02-11 04:31:58.580 7849-8120/com.myapp.app E/EGL_emulation: tid 8120: eglQueryContext(2160): error 0x3004 (EGL_BAD_ATTRIBUTE)
2022-02-11 04:31:58.581 7849-8120/com.myapp.app E/emuglGLESv2_enc: device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glDeleteProgram:2003 GL error 0x501 condition [!isShaderOrProgramObject]
    
    
    --------- beginning of crash
2022-02-11 04:31:58.586 7849-7879/com.myapp.app A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x20 in tid 7879 (RenderThread), pid 7849 (com.myapp.app)

The issue only occurs in galaxy fold devices with android 12 OS. Other devices or even galaxy folds devices with lower versions of android work perfectly fine.

I've already been searching on the web with similar issues, but I could not find one.

If anyone knows how to fix this? Or has any idea what's causing this issue. It would be a big help for my ongoing project.

Thank you.

Here is my implementation of the recycler view adapter.


public class VideoBoxAdapter extends RecyclerView.Adapter<VideoBoxViewHolder> {

    private static final String TAG = Logger.getSimpleName(VideoBoxAdapter.class);

    public static class VideoBoxAdapterParams {
        public ScreenLayoutEvent.LayoutOptions layoutOptions;
        public List<VideoBox> videoBoxes;
        public VideoBoxEvent events;
        public Context context;
    }

    private final VideoBoxAdapterParams params;

    private boolean disableUserControl = false;

    public VideoBoxAdapter(VideoBoxAdapterParams params) {
        this.params = params;
    }

    @NonNull
    @Override
    public VideoBoxViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(params.context);
        View view = layoutInflater.inflate(R.layout.recycler_item_videobox, null, false);
        return new VideoBoxViewHolder(view);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull VideoBoxViewHolder holder, @SuppressLint("RecyclerView") int position) {

        VideoBox videoBox = params.videoBoxes.get(position);
        holder.remote_username.setText(videoBox.getId().split(":::")[1]);

        String textLabel = params.context.getString(videoBox.isOnFocus() ? R.string.on_main_screen : R.string.camera_off);
        holder.remote_label.setText(textLabel);
        holder.remote_video.setOnClickListener(v -> {
            if (!disableUserControl)
                if (videoBox.isOnFocus())
                    params.events.onAlertHind(params.context.getString(R.string.speaker_already_in_main_screen));
                else moveToMainScreen(videoBox, true);
            else params.events.onAlertHind(params.context.getString(R.string.message_mainscreen_is_disable));
        });

        if (!videoBox.isOnFocus()) {
            holder.remote_video.post(() -> {
                ViewGroup parentView = (ViewGroup) videoBox.getSurfaceView().getParent();
                videoBox.setZOverlay(false);
                videoBox.getSurfaceView().setZOrderMediaOverlay(false);
                if (parentView != null) {
                    parentView.removeView(videoBox.getSurfaceView());
                }
                holder.remote_video.addView(videoBox.getSurfaceView());
            });
        } else {
            moveToMainScreen(videoBox, false);
        }

        adjustLayoutWidth(holder.remote_layout, holder.remote_video);
    }

    @Override
    public int getItemCount() {
        return params.videoBoxes == null? 0 : params.videoBoxes.size();
    }

    public void moveToMainScreen(VideoBox videoBox, boolean notifyChange) {
        VideoBoxCollectionsTool.RemoveVideoBoxFromParentView(videoBox);
        params.events.onRemovePrevSelectedVideo();
        params.events.onVideoSelect(videoBox);
        if (notifyChange)
            params.events.onReloadContent();
    }

    public void setDisableUserControl(boolean enable) {
        disableUserControl = enable;
    }

    private void adjustLayoutWidth(ConstraintLayout remoteLayout, FrameLayout remoteVideo) {
        try {
            ViewGroup.LayoutParams mainLayoutParams = remoteLayout.getLayoutParams();
            ViewGroup.LayoutParams videoLayoutParams = remoteVideo.getLayoutParams();
            double widthPixel = ViewManager.GetWidth(params.context.getResources());

            switch (params.layoutOptions) {
                case CHAT_SCREEN_LAYOUT:
                    videoLayoutParams.width = (int) (widthPixel * 0.40);
                    break;
                case TILE_SCREEN_LAYOUT:
                    videoLayoutParams.width = (int) (widthPixel * 0.49);
                    break;
                case FULL_SCREEN_LAYOUT:
                    videoLayoutParams.width = (int) (widthPixel * 0.46);
                    ((ViewGroup.MarginLayoutParams) mainLayoutParams).setMargins(5, 0, 5, 12);
                    break;
            }

            remoteLayout.setLayoutParams(mainLayoutParams);
            remoteVideo.setLayoutParams(videoLayoutParams);
        } catch (Exception e) {
            Logger.error(TAG, "onBindViewHolder(): " + e.getMessage());
        }
    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source