Android Question inline java and javaobject

Chris Guanzon

Active Member
Licensed User
Longtime User
Hello everyone, i am having a problem with inline java and javaobject. I have a co-developer who knows how to code in java, my problem is i don't know how to pass the code to b4a. i can't get the return value of the java code. below is the inline java code:


B4X:
#if java
    import com.vanstone.appsdk.client.ISdkStatue;
    import com.vanstone.trans.api.PrinterApi;
    import com.vanstone.trans.api.SystemApi;
    import com.vanstone.utils.CommonConvert;

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;

    int pflag = 0;
    Activity cActivity;

    public void setTestContext(Activity activity) {
        cActivity = activity;
    }

    public void initialized_api(Activity activity) {
        /*
        new Thread() {
            public void run() {
                SystemApi.SystemInit_Api(0, CommonConvert.StringToBytes(getCurAppDir() + "/" + "\0"), activity, new ISdkStatue() {
                    @Override
                    public void sdkInitSuccessed() {
                        pflag = 1;
                    }

                    @Override
                    public void sdkInitFailed() {
                        pflag = 0;
                    }
                });
            }
        }.start();*/
        
        SystemApi.SystemInit_Api(0, CommonConvert.StringToBytes(getCurAppDir() + "/" + "\0"), activity, new ISdkStatue() {
            @Override
            public void sdkInitSuccessed() {
                pflag = 1;
            }

            @Override
            public void sdkInitFailed() {
                pflag = 0;
            }
        });
    }

    public String getCurAppDir() {
        String CurAppDir = this.getApplicationContext().getFilesDir().getAbsolutePath();

        return CurAppDir;
    }

    public Activity getCurrentContext() {
        return cActivity;
    }

    public int getFlag() {
        return pflag;
    }

    public void PrtCardInfo() {
        PrinterApi.PrnClrBuff_Api();
        PrinterApi.PrnFontSet_Api(32, 32, 0);
        PrinterApi.PrnSetGray_Api(15);
        PrinterApi.PrnLineSpaceSet_Api((short) 5, 0);
        PrinterApi.PrnStr_Api("     POS Receipt");
        PrinterApi.PrnFontSet_Api(24, 24, 0);
        PrinterApi.PrnStr_Api("               CARDHOLDER COPY");
        PrinterApi.PrnStr_Api("--------------------------------");
        PrinterApi.PrnStr_Api("MERCHANT NAME:");
        PrinterApi.PrnStr_Api("CARREFOUR");
        PrinterApi.PrnStr_Api("MERCHANT NO.: 941160147848170");
        PrinterApi.PrnStr_Api("120401124594");
        PrinterApi.PrnStr_Api("TERMINAL NO.: ");
        PrinterApi.PrnStr_Api("TRANS TYPE.: ");
        PrinterApi.PrnFontSet_Api(32, 32, 0);
        PrinterApi.PrnStr_Api("Sale");
        PrinterApi.PrnFontSet_Api(24, 24, 0);
        PrinterApi.PrnStr_Api("PAYMENT TYPE.: ");
        PrinterApi.PrnStr_Api("CARDHOLDER SIGNATURE:\n\n\n\n");
        PrinterApi.PrnStr_Api("--------------------------------");
        PrinterApi.PrnStr_Api("I accept this trade and allow it on my account");
        PrinterApi.PrnStr_Api("----------x------------x-------");
        PrinterApi.PrnStr_Api("\n\n");

        PrinterApi.printAddBarCode_Api(1, 300, 120, "12345");
        PrinterApi.PrnStr_Api("\n\n");
        PrinterApi.printAddQrCode_Api(1, 300, "www.szfptech.com");
        PrinterApi.PrnStr_Api("\n\n\n\n");


        PrintData();
    }

    public static int PrintData() {
        int ret;
        String Buf = null;
        while (true) {
            ret = PrinterApi.PrnStart_Api();
            Log.d("aabb", "PrnStart_Api:" + ret);
            if (ret == 2) {
                Buf = "Return:" + ret + "    paper is not enough";
            } else if (ret == 3) {
                Buf = "Return:" + ret + "    too hot";
            } else if (ret == 4) {
                Buf = "Return:" + ret + "    PLS put it back\nPress any key to reprint";
            } else if (ret == 0) {
                return 0;
            }
            return -1;
        }
    }
#End If
then my code


B4X:
Sub GetActivity As Object
    Dim R As Reflector
    Return R.GetActivity
End Sub

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private NativeMe As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Layout")
    NativeMe.InitializeContext
    NativeMe.RunMethod("initialized_api", Array(GetActivity))
End Sub

I hope someone can help me or teach me how to do it.
 

Chris Guanzon

Active Member
Licensed User
Longtime User
this is the error


B4X:
Error occurred on line: 91 (Main)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    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 anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:5675)
    at android.view.View$PerformClick.run(View.java:22641)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6251)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.NoClassDefFoundError: com.vanstone.trans.api.jni.JFun
    at com.vanstone.trans.api.jni.JFun.SystemInit_jni(Native Method)
    at com.vanstone.trans.api.SystemApi.SystemInit_Api(SystemApi.java:150)
    at com.vanstone.trans.api.SystemApi.SystemInit_Api(SystemApi.java:185)
    at mpos.test.main.initialized_api(main.java:521)
    ... 20 more
Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/mpos.test-2/base.apk"],nativeLibraryDirectories=[/data/app/mpos.test-2/lib/arm, /system/lib, /vendor/lib, /system/vendor/lib, /custom/lib]]] couldn't find "libA90JavahCore.so"
    at java.lang.Runtime.loadLibrary0(Runtime.java:972)
    at java.lang.System.loadLibrary(System.java:1530)
    at com.vanstone.trans.api.jni.JFun.<clinit>(JFun.java:9)
    ... 24 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
did you added the jar with the code with #additionaljar: jarfile ?
 
Upvote 0
Top