Android Question Crash with WebViewAssetLoader + ShouldInterceptRequest (NullPointerException on exit)

khwarizmi

Active Member
Licensed User
Longtime User
Hello,

I am facing a crash when using WebViewAssetLoader with WebViewClient.ShouldInterceptRequest.

🔹 Scenario:​

I load local HTML files (unzipped previously) from File.DirInternal using:
B4X:
WebView1.LoadUrl(WebViewAssetLoader1.InternalPathUrl & "/exp/" & exp)

🔹 Initialization:
B4X:
WebViewAssetLoader1.Initialize(Application.PackageName)


WebViewEx.Initialize(WebView1)
WebViewClient.Initialize("WebViewClient")

WebViewEx.SetWebViewClient(WebViewClient)
WebViewEx.JavaScriptEnabled = True

🔹 Events:​

B4X:
Private Sub WebViewClient_PageFinished(Url As String)

    If Url.StartsWith(WebViewAssetLoader1.AssetPathUrl) Then
        ToastMessageShow("Page loaded from DirAsset", True)
    Else If Url.StartsWith(WebViewAssetLoader1.InternalPathUrl) Then
        ToastMessageShow("Page loaded from DirInternal", True)
    Else If Url.StartsWith(WebViewAssetLoader1.ResourcesPathUrl) Then
        ToastMessageShow("Page loaded from Resources", True)
    Else
        ToastMessageShow("Page loaded from internet", True)
    End If
End Sub

Private Sub WebViewClient_ShouldInterceptRequest(Url As String) As WebResourceResponse

    Log("URL: " & Url)
 
    Dim Response As WebResourceResponse
    Dim ResponseReceived As WebResourceResponse

    Try
        ResponseReceived = WebViewAssetLoader1.ShouldInterceptRequest(Url)
     
        If Not(ResponseReceived = Null) And ResponseReceived.IsInitialized Then
            Response.Initialize(ResponseReceived.GetMimeType, ResponseReceived.GetEncoding, ResponseReceived.GetData)
            Return Response
        End If
    Catch
        Log("URL not found: " & Url)
    End Try
 
    Return Null
End Sub

🔴 Problem:​

When I exit the Activity (or it goes to background), the app crashes with:

java.lang.NullPointerException: Attempt to invoke virtual method
'boolean anywheresoftware.b4a.AbsObjectWrapper.IsInitialized()' on a null object reference
at uk.co.martinpearman.b4a.webkit.DefaultWebViewClient$1.shouldInterceptRequest(...)
 

zed

Well-Known Member
Licensed User
The crash means that the WebViewClient continues to receive requests even though the activity has already been destroyed, and your code is trying to access a B4A object that has already been released.

This happens because:

Android sometimes queues WebView requests.

WebViewAssetLoader also intercepts subrequests (CSS, JS, images).

When the activity is destroyed, the WebView is destroyed, but the Java callback continues.

The B4A wrapper (WebResourceResponse) no longer exists. Therefore, a NullPointerException is thrown.

The simplest solution would be to ignore requests if the activity is no longer active. Add a flag: `Private ActivityIsPaused As Boolean`.

In `Activity_Pause`: `ActivityIsPaused = True`
In `Activity_Resume`: `ActivityIsPaused = False`
And in `ShouldInterceptRequest`: `If ActivityIsPaused Then Return Null`
The crash disappears because you are preventing access to destroyed objects.

But the best solution is to switch to B4XPages.
 
Upvote 0
Top