Skip to content

Fix NullPointerException in setupBroadcast when frameContainerLayout is null (Fixes #274) - #399

Closed
ricardosohn wants to merge 3 commits into
capacitor-community:masterfrom
ricardosohn:master
Closed

ricardosohn wants to merge 3 commits into
capacitor-community:masterfrom
ricardosohn:master

Conversation

@ricardosohn

@ricardosohn ricardosohn commented Nov 26, 2025 •

Copy link
Copy Markdown

Hello,
I have tested the change proposed on #274 to resolve this issue and it worked.
Let me know if anything else is needed in this PR.
From the bug description itself:

Describe the bug
NullPointerException for Android when "toBack" is set to true.

NullPointerException: Attempt to invoke virtual method 'boolean android.widget.FrameLayout.dispatchTouchEvent(android.view.MotionEvent)' on a null object reference

To Reproduce
Steps to reproduce the behavior:

Integrate this plugin
Set "toBack" to true
Running app
When opening a screen that is invoking start of camera preview and user immediately clicks on screen when camera is not finished launching, app crashes
Expected behavior

App does not crash when clicking to early on screen..

Additional context

In the CameraPreview -> setupBroadcast method we invoke fragment.frameContainerLayout.dispatchTouchEvent(event);
This will crash when fragment.frameContainerLayout is null. This is not being checked accordingly. Please, check the current method below.

I think, there should be (null != fragment.frameContainerLayout) added to the if statement.


private void setupBroadcast() {
        /** When touch event is triggered, relay it to camera view if needed so it can support pinch zoom */

        getBridge().getWebView().setClickable(true);
        getBridge()
            .getWebView()
            .setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if ((null != fragment) && (fragment.toBack == true)) {
                            fragment.frameContainerLayout.dispatchTouchEvent(event);
                        }
                        return false;
                    }
                }
            );
    }

Copilot AI and others added 3 commits November 26, 2025 12:17
…is null

Co-authored-by: ricardosohn <2574132+ricardosohn@users.noreply.github.com>
…is null (Fixes capacitor-community#274)

Fix NullPointerException in setupBroadcast when frameContainerLayout is null (Fixes capacitor-community#274)
@ryaa

ryaa commented Sep 25, 2026

Copy link
Copy Markdown
Member

Thanks @ricardosohn for the fix and for identifying this touch-event lifecycle edge case.

The null check proposed here is already present in CameraPreview.setupBroadcast() on master:

if (fragment != null && fragment.toBack && fragment.frameContainerLayout != null) {
    fragment.frameContainerLayout.dispatchTouchEvent(event);
}

This checks that frameContainerLayout exists before forwarding the touch event, providing the same protection as this PR's change. There is therefore no remaining code change to merge from this PR.

Closing as already implemented in master. This confirms that the specific null-dereference path addressed here is guarded; it does not establish that every scenario reported under #274 has been resolved. Thank you for the contribution.

@ryaa ryaa closed this Sep 25, 2026
@ryaa ryaa added this to the v8.0.2 milestone Sep 25, 2026
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.

3 participants