Java Question Event dont Fire

MarcoRome

Expert
Licensed User
Longtime User
Hi all i have this code in my class ( this is a library write in B4A ):

Class pdftotext:

B4X:
#Event: onMessage(Success As String )
'#Event: OnSecond
Private Sub Class_Globals
    Private nativeMe As JavaObject
    Private nome_evento As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(evento As String)
    nome_evento = evento
End Sub

Public Sub Converti(namefilepdf As String, namefiletxt As String)
'    Log(File.DirAssets)
'    Log(File.DirRootExternal)
    nativeMe = Me
    Dim filepdf As String = namefilepdf
    Dim filetxt As String = namefiletxt
    nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, nome_evento))
End Sub



#If Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.*;



private BA ba;
private String eventName;


    public void parsePdf(String filepdf, String filetxt, String nomeevento) throws IOException {
            String pdf = filepdf;
            String txt = filetxt;
             this.eventName = nomeevento.toLowerCase(BA.cul);
            PdfReader reader = new PdfReader(pdf);
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            PrintWriter out = new PrintWriter(new FileOutputStream(txt));
            TextExtractionStrategy strategy;
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
                out.println(strategy.getResultantText());
            }
            reader.close();
            out.flush();
            out.close();
            String Convertito = "Done";                              
            BA.Log("Name Event: " + eventName + "_onmessage");                              
            try {
                if(ba.subExists(eventName + "_onmessage")){
                    BA.Log("Found");
                    ba.raiseEventFromUI(this, eventName.toLowerCase(BA.cul) + "_onmessage", Convertito);
                } else {
                    BA.Log("Not Found");
                }
            } catch (Exception e) {
throw new RuntimeException(e);
}
    }

#End If

i have this code in Activity Main:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim filepdf As String = File.DirRootExternal &"/test-armen.pdf"
    Dim filetxt As String = File.DirRootExternal &"/test.txt"
    Dim pdf As pdftotext
    pdf.Initialize("pdf")
    pdf.Converti(filepdf, filetxt)


End Sub

Sub pdf_onMessage(Success As String )
    Log("Convertion: " & Success)
End Sub

All work but event dont fire. When i run app i have this in my log:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Name Event: pdf_onmessage
Not Found
** Activity (main) Resume **

so The name event is pdf_onmessage and in activity i have this sub.
The JAR file is itextpdf-5-5-6.jar ( click on for download )
In attachment project
Any Suggestion ?
Thank you
 

Attachments

  • PdtToTxt2Rel3.zip
    10.3 KB · Views: 265

DonManfred

Expert
Licensed User
Longtime User
in you call you create the eventsub
ewventname_onmessage

inside this event you call a sub in your activity.
 

LucaMs

Expert
Licensed User
Longtime User
Can you make a little example ?
Thank you Erel
Non capisco come mai tu ti sia perso in un bicchier d'acqua alla riposta di Erel, visto quanto sei in gamba!!!

L'evento non andrà direttamente al modulo (Activity, in questo caso) ma alla tua classe, quindi dall'interno di questa tu devi "girare" l'evento all'Activity.
Insomma, la routine evento la metti nella classe e dall'interno di questa fai una callsub ad un altro evento che dovrà stare nell'Activity (o meglio nel Callback, perché potrebbe anche essere un modulo diverso, un'altra classe, ad esempio).

Quello che non ho capito è come mai Erel dica:
(assuming that there is one)
come se se ne potesse fare a meno.

--------

Sorry for the above, I'm able to explain better in italian what I mean; in short, I tried to say that it is a kind of delegation: Marco's class must contain the event routine which in turn will call the routine placed in the callback.

But I don't understand this:
on the Callback parameter (assuming that there is one).
Is not a callback always necessary (if there are any events)?
 

MarcoRome

Expert
Licensed User
Longtime User
Oh boy, maybe i'm tired (surely) but i swear ..."I didn't understand"
You can give me an example on my code above please
We have in # 1 source
Thanks a lot
 

LucaMs

Expert
Licensed User
Longtime User
You can give me an example on my code above please

I think it should be:

B4X:
Private Sub Class_Globals
    Private mCallback As Object
    Private nome_evento As String


Public Sub Initialize(callback As Object, evento As String)
    mCallback = callback
    nome_evento = evento


Public Sub Converti(namefilepdf AsString, namefiletxt AsString)
    '...
    ' nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, nome_evento))
    nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, "eventointerno"))


Sub eventointerno_onMessage(Success As String)
    Log("Convertion: " & Success)
    Dim FullRoutineName As String = nome_evento & "_onMessage"
    If SubExists(mCallBack, FullRoutineName) Then
        CallSub2(mCallBack, FullRoutineName, Success)
    End If
End Sub
 

MarcoRome

Expert
Licensed User
Longtime User
I think it should be:

B4X:
Private Sub Class_Globals
    Private mCallback As Object
    Private nome_evento As String


Public Sub Initialize(callback As Object, evento As String)
    mCallback = callback
    nome_evento = evento


Public Sub Converti(namefilepdf AsString, namefiletxt AsString)
    '...
    ' nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, nome_evento))
    nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, "eventointerno"))


Sub eventointerno_onMessage(Success As String)
    Log("Convertion: " & Success)
    Dim FullRoutineName As String = nome_evento & "_onMessage"
    If SubExists(mCallBack, FullRoutineName) Then
        CallSub2(mCallBack, FullRoutineName, Success)
    End If
End Sub

I have this error:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Name Event: eventointerno_onmessage
Found
** Activity (main) Resume **
Convertion: Done
pdftotext_eventointerno_onmessage (B4A line: 26)
If SubExists(mCallback, FullRoutineName) Then
java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
at anywheresoftware.b4a.keywords.Common.getComponentBA(Common.java:1184)
at anywheresoftware.b4a.keywords.Common.SubExists(Common.java:957)
at library.pdftotext.pdftotext._eventointerno_onmessage(pdftotext.java:101)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA$1.run(BA.java:325)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

upload_2017-7-10_21-29-52.png
 

Attachments

  • upload_2017-7-10_21-29-21.png
    upload_2017-7-10_21-29-21.png
    450.6 KB · Views: 239

MarcoRome

Expert
Licensed User
Longtime User
Yes work.
I've already created a wrapper for this (in standard mode HERE ) but i was curious if the same thing could only be done with B4A.
In any case now i have two conclusions:
1. Yes is possible
2. It's official I need a couple of weeks of vacation :(:):D

Thank you all for your support
 
Top