Android Question Calling Java Class

prajinpraveen

Active Member
Licensed User
Longtime User
Hello Everyone,

I am trying to convert the following java code to B4A

1:
PrintTask printTask = new PrintTask();
printTask.setPrintBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.printer));
Printer.getInstance().startPrint(printTask, new PrinterCallback() {

2:
// PrinterTask.Java

package com.pos.device.printer;

import android.graphics.Bitmap;

public class PrintTask {
  public int mStep;
 
  public Bitmap mBitmap;
 
  public PrintTask() {
    throw new RuntimeException();
  }
 
  public int getGray() {
    throw new RuntimeException();
  }
 
  public void setGray(int gray) {
    throw new RuntimeException();
  }
 
  public void addFeedPaper(int step) {
    throw new RuntimeException();
  }
 
  public void setPrintCanvas(PrintCanvas printCanvas) {
    throw new RuntimeException();
  }
 
  public void setPrintBitmap(Bitmap origBitmap) {
    throw new RuntimeException();
  }

  public void setPrintBuffer(byte[] buffer) {
    throw new RuntimeException();
  }
}

B4X:
    Dim bitmap As B4XBitmap = TextToBitmap("Text to Bitmap", 20, xui.Color_Black, "CENTER")
    Dim jo As JavaObject
    Try
        jo = jo.InitializeNewInstance("com.pos.device.printer.PrintTask",Null)
        jo.RunMethod("setPrintBitmap", Array As Object(bitmap))
    Catch
        Log(LastException)
    End Try


java.lang.RuntimeException
    at com.pos.device.printer.PrintTask.<init>(PrintTask.java:20)
    at java.lang.Class.newInstance(Native Method)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:86)
    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:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.test.sfpayments.main._print(main.java:496)
    at com.test.sfpayments.main._activity_create(main.java:488)
    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:157)
    at com.test.sfpayments.main.afterFirstLayout(main.java:121)
    at com.test.sfpayments.main.access$000(main.java:33)
    at com.test.sfpayments.main$WaitForLayout.run(main.java:99)
    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:6121)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1059)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
(Exception) java.lang.Exception:  java.lang.RuntimeException

InitializeStatic initialises the class, but cannot run the SetPrintBitmap Method


3:
    Dim bitmap As B4XBitmap = TextToBitmap("Text to Bitmap", 20, xui.Color_Black, "CENTER")
    Dim jo As JavaObject
    Try
        jo = jo.InitializeStatic("com.pos.device.printer.PrintTask")
        jo.RunMethod("setPrintBitmap", Array As Object(bitmap))
    Catch
        Log(LastException)
    End Try
    
    Log(jo)
    
    Error occurred on line: 592 (Main)
java.lang.IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    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:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.test.sfpayments.main._print(main.java:496)
    at com.test.sfpayments.main._activity_create(main.java:488)
    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:157)
    at com.test.sfpayments.main.afterFirstLayout(main.java:121)
    at com.test.sfpayments.main.access$000(main.java:33)
    at com.test.sfpayments.main$WaitForLayout.run(main.java:99)
    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:6121)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1059)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
(Exception) java.lang.Exception:  java.lang.IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
(Class) class com.pos.device.printer.PrintTask

Any assistance will be greatly appreciated.

Thank you
 

DonManfred

Expert
Licensed User
Longtime User
This can not be the right code. All the code is doing is to create a RuntimeException.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User


Is the code editor collapsed?
Can you click the minus button to reveal the code that may be hidden?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Can you click the minus button to reveal the code that may be hidden?
i guess the decompile just fails to decompile the jar and just create this methods.
A point more not to use a decompiler and instead use the sourcejars if there are available....
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
InitializeStatic initialises the class, but cannot run the SetPrintBitmap Method
I think InitializeNewInstance is correct.

java.lang.RuntimeException at com.pos.device.printer.PrintTask.<init>(PrintTask.java:20)
Need to figure out this error.


B4X:
    Try
        Dim jo As JavaObject
        jo.InitializeNewInstance("com.pos.device.printer.PrintTask", Null)
        jo.RunMethod("setPrintBitmap", Array As Object(bitmap))
    Catch
        Log(LastException)
    End Try
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Maybe try
B4X:
    Dim pt As JavaObject
    pt.InitializeStatic("com.pos.device.printer.PrintTask")
    pt.RunMethod("setPrintBitmap",Array("load_your_image_here"))
 
Upvote 0

prajinpraveen

Active Member
Licensed User
Longtime User
Thank you.

I have tried this. gives me the following error


Error occurred on line: 592 (Main)
java.lang.IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
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:157)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at com.test.sfpayments.main._print(main.java:496)
at com.test.sfpayments.main._activity_create(main.java:488)
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:157)
at com.test.sfpayments.main.afterFirstLayout(main.java:121)
at com.test.sfpayments.main.access$000(main.java:33)
at com.test.sfpayments.main$WaitForLayout.run(main.java:99)
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:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1059)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
(Exception) java.lang.Exception: java.lang.IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
(Class) class com.pos.device.printer.PrintTask
 
Upvote 0

aeric

Expert
Licensed User
Longtime User

Usually I will try to Log the object to see it is successfully initialized.
B4X:
Try
    Dim jo As JavaObject
    jo.InitializeNewInstance("com.pos.device.printer.PrintTask", Null)
    Log(jo)
Catch
    Log(LastException)
End Try
 
Upvote 0

prajinpraveen

Active Member
Licensed User
Longtime User

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
printing
Error occurred on line: 582 (Main)
java.lang.RuntimeException
at com.pos.device.printer.PrintTask.<init>(PrintTask.java:20)
at java.lang.Class.newInstance(Native Method)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:86)
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:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at za.co.irays.sfpayments.main._print(main.java:506)
at za.co.irays.sfpayments.main._activity_create(main.java:498)
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:157)
at za.co.irays.sfpayments.main.afterFirstLayout(main.java:131)
at za.co.irays.sfpayments.main.access$000(main.java:43)
at za.co.irays.sfpayments.main$WaitForLayout.run(main.java:109)
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:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1059)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
** Activity (main) Resume **
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
IllegalArgumentException: Expected receiver of type com.pos.device.printer.PrintTask, but got java.lang.Class<com.pos.device.printer.PrintTask>
There is an Receiver expected here.

You need to have an receiver for PrintTask i guess.
 
Upvote 0