Java Question Java inline createevent

EduardoElias

Well-Known Member
Licensed User
Longtime User
Hi there!

I need to create events from inline java code from a wrapper i am doing.

I am not familiar with JAVA and there is no proper documentation available

B4X:
    @Override
    public void onTransactionDone(HashMap<String, String> hashMap) {
        BA.Log("Transaction.onTransactionDone ");

        switch(getTransactionType()) {
            case ativacao:
                BA.Log("Transaction.onTransactionDone ATIVACAO");
                // hashMap vem vazio para ATIVACAO
                break;

            case comprovante:
                BA.Log("Transaction.onTransactionDone COMPROVANTE");

                // hashMap vem com comprovantes
                comprovanteEstabelecimento = hashMap.get("via_estabelecimento");

                BA.raiseEvent(this, "yNexu_Comprovante", comprovanteEstabelecimento);

this inline java is part of a B4A class module

I need to call, preferable in the same class module a event passing a string;

the way it is now I get:
B4X:
src\yashar\yTEF\ynexu.java:374: error: non-static method raiseEventFromUI(Object,String,Object...) cannot be referenced from a static context
                BA.raiseEventFromUI(this, "yNexu_Comprovante", comprovanteEstabelecimento);

I have tried:
B4X:
                BA ba = (BA) this.getClass().getField("processBA").get(null);
                ba.raiseEvent(this, "yNexu_Comprovante", comprovanteEstabelecimento);

and give me this error:
B4X:
src\yashar\yTEF\ynexu.java:374: error: unreported exception NoSuchFieldException; must be caught or declared to be thrown
                BA ba = (BA) this.getClass().getField("processBA").get(null);
                                                     ^

and some other variances, what is the proper way to call for event from the inline jave from class module ?
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
If you don't know Java then in most cases it will be easier to use JavaObject. There are some limits, including that the callback must be an interface and not an abstract class.
I have already done all the wrapper and everything is working now.

The JAVA code is extense to change now.

Except this raiseEvent, is there a workaround so that I can pass the values from the JAVA events?
 

EduardoElias

Well-Known Member
Licensed User
Longtime User

Based on this example I could compile, however the event is never fired:

B4X:
Sub Class_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private nexupayment As JavaObject
    Private start As JavaObject
    Private contextActivity As JavaObject
    Private Context As JavaObject
    Private FProducao As Boolean
    Private FMain As Object
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(aMain As Object)
    FMain = aMain
    Context = GetContext
    start.InitializeContext

    Log(Application.PackageName)
    nexupayment = start.InitializeNewInstance(Application.PackageName&".ynexu.nexutransaction", Array(Me))
   
    nexupayment.RunMethod("Start", Array(Context))
End Sub

Sub setProducao(aProducao As Boolean)
    FProducao = aProducao
End Sub

Sub getProducao As Boolean
    Return FProducao
End Sub

private Sub Preparar
    contextActivity.InitializeContext
    nexupayment.RunMethod("Preparar", Array(Context, contextActivity, FProducao))
End Sub

Sub Ativar(CPF As String)
    Preparar
    nexupayment.RunMethod("Ativar", Array(CPF))
End Sub

Sub Pagar(ValorPagamento As Double, FormaPagamento As String, NumeroParcelas As Int, ComJuros As Boolean)
    Preparar
    nexupayment.RunMethod("Pagar", Array(ValorPagamento, FormaPagamento, NumeroParcelas, ComJuros))
End Sub

Sub GetContext As JavaObject
    Return GetBA.GetField("context")
End Sub

private Sub GetBA As JavaObject
    Dim jo As JavaObject
    Dim cls As String = FMain
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetFieldJO("processBA")
End Sub


private Sub yNexu_comprovante(comp As String)
    Log(comp)
End Sub

#if java
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.*;

import android.content.Context;
import android.app.Activity;

import java.util.HashMap;
import java.util.List;

import ca.nexu.nexupayment.NexuPayment;
import ca.nexu.nexupayment.NexuPaymentPrinterListener;
import ca.nexu.nexupayment.NexuPaymentTransactionListener;

public static class nexutransaction implements NexuPaymentTransactionListener, NexuPaymentPrinterListener {

    private String     comprovanteEstabelecimento;
    private String     comprovanteCliente;
    private String     comprovanteClienteSMS;
    private String     relatorioTransacoes;
    private String    nsu;

    private BA ba;

    public nexutransaction(B4AClass parent) {
        ba = parent.getBA();
    }

    enum    TransactionType {
        ativacao,
        pagamento,
        solicitarPreAut,
        confirmarPreAut,
        cancelamento,
        comprovante,
        relatorios;
    };

    private TransactionType                transactionType;

    public void setTransactionType(TransactionType transactionType) {
        this.transactionType = transactionType;
    }
   
    public TransactionType getTransactionType() {
        return this.transactionType;
    }

    public NexuPayment nexuPayment;

    public void Start(Context c) {
        BA.Log("Transaction.Ativar loadSDK");

        NexuPayment.loadSDK(c);
    }
   
    public void Preparar(Context c, Activity a, boolean Producao) {
        BA.Log("Transaction.Prepare");
   
         nexuPayment = new NexuPayment(this, c, a, null, null);

        // servidor producao
        if (Producao) {
            BA.Log("Transaction.Preparar definirServidor");
            nexuPayment.definirServidor("epico.nexu.cloud", 60906);
        }

        nexuPayment.configurar("1.1.0", "DASHEN", "**** DASHEN ****", null);

    }

    public void Pagar(double anValorPagamento, String asFormaPagamento, int anNumeroParcelas, boolean abComJuros) {
        BA.Log("Transaction.Pagar pagar");

        setTransactionType(TransactionType.comprovante);
        nexuPayment.pagar(anValorPagamento, asFormaPagamento, anNumeroParcelas, abComJuros);
    }

    public void Ativar(String CPF) {
        BA.Log("Transaction.Ativar NexuPayment.ativar "+CPF);
        setTransactionType(TransactionType.ativacao);
   
        nexuPayment.ativar(CPF);
    }


    @Override
    public void onPrinterError(String s) {
    }
   
    @Override
    public void onConfig() {
        BA.Log("Transaction.onConfig");
    }

    @Override
    public void onBegin() {
        BA.Log("Transaction.onBegin");
    }

    @Override
    public void onProgress(String progressMessage) {
        BA.Log("Transaction.onProgress "+progressMessage);
    }

    @Override
    public void onBeforeDone() {
        BA.Log("Transaction.onBeforeDone");
    }

    @Override
    public void onTransactionDone(HashMap<String, String> hashMap) {
        BA.Log("Transaction.onTransactionDone ");

        switch(getTransactionType()) {
            case ativacao:
                BA.Log("Transaction.onTransactionDone ATIVACAO");
                // hashMap vem vazio para ATIVACAO
                break;

            case comprovante:
                BA.Log("Transaction.onTransactionDone COMPROVANTE");

                // hashMap vem com comprovantes
                comprovanteEstabelecimento = hashMap.get("via_estabelecimento");

                //BA ba = (BA) this.getClass().getField("processBA").get(null);
                //ba.raiseEvent(this, "yNexu_Comprovante", comprovanteEstabelecimento);

                comprovanteCliente = hashMap.get("via_cliente");
                comprovanteClienteSMS = hashMap.get("via_cliente_sms");
                nsu = hashMap.get("transacao_nsu");
               
                ba.raiseEvent(this, "yNexu_comprovante", comprovanteCliente);

//                BA.Log(comprovanteEstabelecimento);
                break;

            case pagamento:
            case solicitarPreAut:
            case confirmarPreAut:
            case cancelamento: {
                BA.Log("Transaction.onTransactionDone CANCELAMENTO");

                // hashMap vem com comprovantes
                comprovanteEstabelecimento = hashMap.get("via_estabelecimento");
                comprovanteCliente = hashMap.get("via_cliente");
                comprovanteClienteSMS = hashMap.get("via_cliente_sms");
                break;
            }

            case relatorios: {
                BA.Log("Transaction.onTransactionDone RELATORIOS");

                // hashMap vem com relatorio
                relatorioTransacoes = hashMap.get("relatorio_transacoes");;
                break;
            }

        }

    }
   
    @Override
    public void onEnd() {
        BA.Log("Transaction.onEnd");
    }

    @Override
    public void onAbort(int abortCode) {
        BA.Log("Transaction.onAbort"+abortCode);
    }

    @Override
    public void onDebug(String s) {
        BA.Log("Transaction.onDebug "+s);
    }

    @Override
    public void onShowPinpad(boolean b) {
    }

    @Override
    public void onSetKeyboard() {
    }

    @Override
    public void onDisplayPinpadMessage(List<String> list) {
        // to be implemented
    }
   

}

#End If

used ba.raiseEventFromUI and ba.raiseEvent on both cases are called but no event in B4A (where I added a breakpoint and a Log)

B4X:
    private BA ba;

    public nexutransaction(B4AClass parent) {
        ba = parent.getBA();
    }

this is the B4A sub event:
B4X:
private Sub yNexu_comprovante(comp As String)
    Log(comp)
End Sub

and how I am calling :
B4X:
                ba.raiseEvent(this, "yNexu_comprovante", comprovanteCliente);

on the InitializeNewInstace ai am now passing me for the constructor:
B4X:
    nexupayment = start.InitializeNewInstance(Application.PackageName&".ynexu.nexutransaction", Array(Me))

What could be the cause?
 
Last edited:

EduardoElias

Well-Known Member
Licensed User
Longtime User
Problem solved !

it is necessary to call de raiseevent all lower case !


ba.raiseEvent(this, "ynexu_comprovante", comprovanteCliente);
 
Top