Android Question WebView with Upload File crashes

Martin Larsen

Active Member
Licensed User
Longtime User
I am trying to implement file upload for a webview.

As soon as I insert these entries in the manifest, the app crashes immediately upon start. I have not implemented any of the B4A and Java code from the example, and the activity with the webview is not even started.

B4X:
AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

This is the original manifest

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
'SetApplicationAttribute(android:windowSoftInputMode, adjustPan|stateHidden)
SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

SetApplicationAttribute(android:theme, "@style/MyAppTheme")

CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#2722C5</item>
        <!--<item name="colorPrimary">#0098FF</item>-->
        <item name="colorPrimaryDark">#007CF5</item>
        <item name="colorAccent">#AAAA00</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowTranslucentNavigation">false</item>
         <item name="android:windowTranslucentStatus">false</item>
    </style>
</resources>
)

And here is the Java exception. It seems to have something to do with AppCompat, but I can figure out in what way:

B4X:
java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.vertikal.kattensvaern/dk.vertikal.kattensvaern.main}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setId(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6680)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setId(int)' on a null object reference
    at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:507)
    at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:280)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
    at dk.vertikal.kattensvaern.main.onCreate(main.java:61)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
    ... 11 more
 

Martin Larsen

Active Member
Licensed User
Longtime User
If I remove this line from the main activity, the exception above does not occur. But it crashes another place though when it tries to load the AppCompat drawer.

B4X:
#Extends: android.support.v7.app.AppCompatActivity

But it does suggest that it has something to do with AppCompat.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I will check the AppCompat library version.
even if you have 4.0.

Download it again. There is a new download for the 4.0 incl. androidX Artifact.
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
I solved the crash in #1. I don't know exactly what I did as I tried several things, but perhaps it was reinstalling AndroidX in SDK Manager.

However, now I have a new problem.

I have copied both manifest entries, java code and B4X code from the webview example. It then crashes on the third line below with java.lang.ClassNotFoundException: dk$vertikal$kattensvaern$main$MyChromeClient:

B4X:
    Activity.LoadLayout("webview")
    Dim client As JavaObject
    client.InitializeNewInstance(Application.PackageName & ".main$MyChromeClient", Null) ' <= CRASHES!
    Dim jo As JavaObject = webview1
    jo.RunMethod("setWebChromeClient", Array(client))
    webview1.LoadUrl("https://b4x.com:51041/websocket_with_fileupload/index.html")

The java exception:

B4X:
** Activity (webviewactivity) Create, isFirst = true **
Error occurred on line: 42 (WebviewActivity)
java.lang.ClassNotFoundException: dk$vertikal$kattensvaern$main$MyChromeClient
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:288)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:83)
    at dk.vertikal.kattensvaern.webviewactivity._activity_create(webviewactivity.java:389)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at dk.vertikal.kattensvaern.webviewactivity.afterFirstLayout(webviewactivity.java:107)
    at dk.vertikal.kattensvaern.webviewactivity.access$000(webviewactivity.java:20)
    at dk.vertikal.kattensvaern.webviewactivity$WaitForLayout.run(webviewactivity.java:85)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6680)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
** Activity (webviewactivity) Resume **

I can't figure out the reason for this as it doesn't happen in the webview upload example project.
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
It then crashes on the third line below with java.lang.ClassNotFoundException: dk$vertikal$kattensvaern$main$MyChromeClient

I have finally found the cause. Actually there are two. The activity is called WebviewActivity so the offending line should be:

B4X:
client.InitializeNewInstance(Application.PackageName & ".webviewactivity$MyChromeClient", Null)
  1. The name before the dollar sign should be the same as the calling activity.
  2. the name must be lower case even if the the activity is CamelCase etc.
@Erel: Please add these two facts to the guide as they are not obvious.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Then I don't know what Erel meant with You shouldn't see android.support classes anymore.
You shouldn't see these classes in error messages.
The compiler, when it can, automatically converts Android Support classes to AndroidX classes, so it is fine to keep using "#Extends: android.support.v7.app.AppCompatActivity" in the activities.

It is equivalent to writing:
B4X:
#Extends: androidx.appcompat.app.AppCompatActivity
 
Upvote 0
Top