Android Question Java Download Listener in a Non-Main Activity

DawningTruth

Active Member
Licensed User
The following code was derived from this forum. It works well in a standard Main activity. However when I try to migrate it to an Activity with a different name, then it fails to find the Java Class. Can someone please assist?

B4X:
'Download Listener

Sub SetupDlS
    
    Dim dl As JavaObject
    dl.InitializeNewInstance(Application.PackageName & ".Main$MyDownloadListener", Null) '***** Error Occurs on this Line
    dl.RunMethod("set", Array(myWebView))
        
End Sub

Sub onDownloadStart (url As String, userAgent As String, contentDisposition As String, mimeType As String, contentLength As Long)
    Log("On Download Start")
    Log("url: " & url)
    Log("userAgent: " & userAgent)
    Log("contentDisposition: " & contentDisposition)
    Log("mimeType: " & mimeType)
    Log("contentLength: " & contentLength)
    
    StartActivity(phoneIntent_.OpenBrowser(url))
    
End Sub

#if Java

public static class MyDownloadListener implements android.webkit.DownloadListener
{

    public void set(android.webkit.WebView wv)
    {
           wv.setDownloadListener(this);
    }

    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
    {
       BA.Log("Java onDownloadStart");
       processBA.raiseEventFromUI(this, "ondownloadstart", url, userAgent, contentDisposition, mimetype, contentLength);
    }

}

#End If

This is the error I'm getting:


Error occurred on line: 854 (viewsite)
java.lang.ClassNotFoundException: b4a$example$Main$MyDownloadListener
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at java.lang.Class.forName(Class.java:378)
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:273)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:83)
at b4a.example.viewsite._setupdls(viewsite.java:1629)
at b4a.example.viewsite._loadwebsite(viewsite.java:675)
at b4a.example.viewsite._activity_create(viewsite.java:467)
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 b4a.example.viewsite.afterFirstLayout(viewsite.java:106)
at b4a.example.viewsite.access$000(viewsite.java:19)
at b4a.example.viewsite$WaitForLayout.run(viewsite.java:84)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.ClassNotFoundException: Didn't find class "b4a$example$Main$MyDownloadListener" on path: DexPathList[[zip file "/data/app/b4a.example-Uez3jBgdBlxazh-05JFj8w==/base.apk"],nativeLibraryDirectories=[/data/app/b4a.example-Uez3jBgdBlxazh-05JFj8w==/lib/arm64, /system/lib64, /vendor/lib64, /product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 24 more
** Activity (viewsite) Resume **
 

DonManfred

Expert
Licensed User
Longtime User
The following code was derived from this forum. It works well in a standard Main activity. However when I try to migrate it to an Activity with a different name, then it fails to find the Java Class.
How do you define it in another activity than Main? The code you posted uses Main Activity, not any other!?
 
Upvote 0

DawningTruth

Active Member
Licensed User
Thx, changed it to:

B4X:
dl.InitializeNewInstance(Application.PackageName & ".viewSite.MyDownloadListener", Null)

Still get the same error. Is there perhaps something wrong with my syntax?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Still get the same error
when the eror is the SAME then you did not changed anything. Probably the spelling is not correct.

How is the generated java file named for QuickviewSite?

QuickviewSite.java?
Quickviewsite.java?
quickviewsite.java?

Edit the java and check the spelling of the main class...

try all lowercased. quickviewsite
 
Upvote 0

DawningTruth

Active Member
Licensed User
I found a file called:

LS_quickviewsite.java

It's contents are:

B4X:
package b4a.example.designerscripts;
import anywheresoftware.b4a.objects.TextViewWrapper;
import anywheresoftware.b4a.objects.ImageViewWrapper;
import anywheresoftware.b4a.BA;


public class LS_quickviewsite{

public static void LS_general(java.util.LinkedHashMap<String, anywheresoftware.b4a.keywords.LayoutBuilder.ViewWrapperAndAnchor> views, int width, int height, float scale) {
anywheresoftware.b4a.keywords.LayoutBuilder.setScaleRate(0.3);
anywheresoftware.b4a.keywords.LayoutBuilder.scaleAll(views);

}
}
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
this is the complete file? What kind of module is the quickviewsite?

Is it named LS_quickviewsite and not quickviewsite as you said earlier??

Anyway; i´m out for work now. You need to wait for other answers.

PS: Check the files in Objects\src\b4a\example <. packagename... Do NOT check the files in the Folder Objects\src\b4a\example\designerscripts
 
Upvote 0

DawningTruth

Active Member
Licensed User
Thx for your patience, I obviously have no idea how the under the hood java bits work with B4A. I found another file:

quickviewsite.java

Which contains the line:

B4X:
public class quickviewsite extends Activity implements B4AActivity{

And changed the line in my code to:

B4X:
dl.InitializeNewInstance(Application.PackageName & ".quickviewsite$MyDownloadListener", Null)

And now it works.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top