Android Question error when using web view and webviewextra

Discussion in 'Android Questions' started by ArminKH, Jul 11, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    hi what is this error?
    photo_2015-07-12_14-24-57.jpg
     
  2. ArminKH

    ArminKH Well-Known Member

    This error just happen on android +5
    my test device is galaxy s5
     
  3. warwound

    warwound Expert Licensed User

    Your javascript is using the CallSub command to call a b4a sub from javascript?
    CallSub has a boolean parameter that must be set to true is the b4a sub (callled from javascript) modifies your activity's UI.
    I suspect you are passing False instead of True to CallSub in your javascript.
     
    ArminKH likes this.
  4. ArminKH

    ArminKH Well-Known Member

    hi thank u
    here is my code
    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: B4A Example
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    Sub Process_Globals

    End Sub

    Sub Globals
        
    Dim JavaScript As WebViewExtras
        
    Private WebView1 As WebView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        WebView1.Initialize(
    "WebView1")
        
    Activity.AddView(WebView1,0,0,100%x,100%y)
        JavaScript.addJavascriptInterface(WebView1,
    "B4A")
        WebView1.LoadUrl(
    "http://gozine2.ir/Specialforms/viewapproximation.aspx")
    End Sub
    Sub Activity_Resume

    End Sub
    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
    Sub WebView1_PageFinished (Url As String)
        
    Log("FINISHED")
        JavaScript.executeJavascript(WebView1,
    "B4A.CallSub('Process_HTML', false, document.documentElement.outerHTML)")
    End Sub

    Sub Process_HTML(Html As String)
        
    Log("Extracted")
        after
    End Sub

    Sub after
        JavaScript.executeJavascript(WebView1,
    "document.getElementById('MainContent_cmbField').value='"&"3"&"';")
        
    ToastMessageShow("set",False)
    End Sub
    when i using this code above error accured but when i call the "after" sub by using
    Code:
    CallSubDelayed(Me,"after")
    then my problem solved
    i found this solution 1 hour ago after 25 compile
    but i think as you said thats better if i set the FALSE TO TRUE in my code
    but can u please explain a bit for me about this story?
    thank u ;)
     
  5. warwound

    warwound Expert Licensed User

    The android user interface must only be modified by code running in the main application thread - it must not be modified by code running in a background thread.
    The JavascriptInterface might or might not be executed in the main application thread - there is no guarantee that it will always be executed in any particular thread.

    If your javascript calls a b4a sub that does not modify the UI then the value of the JavascriptInterface CallSub 'callUIThread' parameter is unimportant - a value of True or False can be used:

    Code:
    Sub after
      
    Dim i As Int
      i=
    123
    End Sub
    The 'after' sub here does not modify the UI.
    BUT your 'after' sub executes javascript that changes the content of a form field being displayed in the WebView - the UI is being modified.
    So you must tell the JavascriptInterface that the UI is going to be modified in the sub called by CallSub.
    The JavascriptInterface then ensures that the b4a sub is called in the main application thread.

    So modify the javascript CallSub statement so it passes true as the callUIThread parameter and you'll have no need to use CallSubDelayed:

    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Dim JavaScript As WebViewExtras
        
    Private WebView1 As WebView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        WebView1.Initialize(
    "WebView1")
        
    Activity.AddView(WebView1,0,0,100%x,100%y)
        JavaScript.addJavascriptInterface(WebView1,
    "B4A")
        WebView1.LoadUrl(
    "http://gozine2.ir/Specialforms/viewapproximation.aspx")
    End Sub
    Sub Activity_Resume

    End Sub
    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
    Sub WebView1_PageFinished (Url As String)
        
    Log("FINISHED")
        JavaScript.executeJavascript(WebView1,
    "B4A.CallSub('Process_HTML', true, document.documentElement.outerHTML)")
    End Sub

    Sub Process_HTML(Html As String)
        
    Log("Extracted")
        JavaScript.executeJavascript(WebView1,
    "document.getElementById('MainContent_cmbField').value='"&"3"&"';")
        
    ToastMessageShow("set",False)
    End Sub
    Martin.
     
    ArminKH and DonManfred like this.
  6. ArminKH

    ArminKH Well-Known Member

    Thank u martin now undestood
    Thanx for complete explain :)
     
  7. Rick Harris

    Rick Harris Well-Known Member Licensed User

    Hello Martin,

    Since a couple of days I am also getting an error with Webviewextras. It crashes my app:
    What could the cause be?

    java.lang.NullPointerException
    uk.co.martinpearman.b4a.webviewextras.WebViewExtras$1.onGeolocationPermissionsShowPrompt(WebViewExtras.java:168)
    at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:558)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5039)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
     
  8. warwound

    warwound Expert Licensed User

    I'd guess that you're loading a webpage that tries to get the user's location but have not implemented the required sub in b4a.
    Guessing that WebViewExtras tries to call a callback sub named ????_GeolocationPermissionsRequest, fails to find the sub and raises the null pointer exception.

    Take a look at this post: http://www.b4x.com/android/forum/threads/webviewextras.12453/#post-102448
    There's a few steps required in order to support webpages that request geolocation.
     
    DonManfred and ArminKH like this.
  9. Rick Harris

    Rick Harris Well-Known Member Licensed User

    Thank you, but this is not the cause. I will have to search further. The problem is that my app crashes before the debugger reports back which line is causing the error.
     
Loading...