Android Question [SOLVED] AdditionalJar RunMethod Problem

Asoka

Member
Licensed User
Longtime User
Im working on a kiosk app, and I must print on an USB connected printer (NP-F3692).
I want to use the manufacturer's lib Called "NPrinterLib.jar":


B4X:
#Region  Project Attributes
    #ApplicationLabel: Nii Print
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#AdditionalJar: NPrinterLib
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Dim Ni As NPrinterLib
    Dim NiOBJ As JavaObject
    Dim M As JavaObject
End Sub

But when I Call a function:

B4X:
Sub OpenPrinter
  
  
    Dim R As Object
  
      
  
    M = NiOBJ.Initializestatic("npi.sdk.NPrinterLib")                    'Initialized OK
    Log(NiOBJ.IsInitialized)
  
    R = M.RunMethod ("NOpenPrinter", Array("PrinterName", False))
  
    'Log (R)

  
End Sub

I see this error:

B4X:
Logger connected to:  HARDKERNEL Co., Ltd. ODROID-XU3
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
true
main_openprinter (java line: 545)
java.lang.IllegalArgumentException: expected receiver of type npi.sdk.NPrinterLib, but got java.lang.Class<npi.sdk.NPrinterLib>
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at asisoft.nii.print.main._openprinter(main.java:545)
    at asisoft.nii.print.main._cmdopen_click(main.java:390)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:4438)
    at android.view.View$PerformClick.run(View.java:18422)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
--------- beginning of /dev/log/system

The function and function arguments seems good.
Can Help Me?
 

DonManfred

Expert
Licensed User
Longtime User
Where is the jar? Where is the Documentation?
Can you upload jar, documentation and example (android studio or similar)?
 
Upvote 0

Asoka

Member
Licensed User
Longtime User
...or use as additional lib (but I haven't NPrinterLib.xml)
Can I generate xml file for this?
 
Upvote 0

Asoka

Member
Licensed User
Longtime User
InitializeNewInstance :
B4X:
M = NiOBJ.InitializeNewInstance("npi.sdk.NPrinterLib", Null)

And It says:
B4X:
Logger connected to:  HARDKERNEL Co., Ltd. ODROID-XU3
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_openprinter (java line: 541)
java.lang.InstantiationException: can't instantiate class npi.sdk.NPrinterLib; no empty constructor
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:85)
    at asisoft.nii.print.main._openprinter(main.java:541)
    at asisoft.nii.print.main._cmdopen_click(main.java:390)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:4438)
    at android.view.View$PerformClick.run(View.java:18422)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
--------- beginning of /dev/log/system
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
java.lang.InstantiationException: can't instantiate class npi.sdk.NPrinterLib; no empty constructor
That's to be expected, since the docs you linked to in post #4 show (page 15) that the class constructor needs to be called with the applications context, but you are calling it with no parameters at all (empty constructor).
Try
B4X:
Dim context As JavaObject
context.InitializeContext
Dim M as JavaObject
M = NiOBJ.InitializeNewInstance("npi.sdk.NPrinterLib", Array As Object(context))
 
Upvote 0

Asoka

Member
Licensed User
Longtime User
That's to be expected, since the docs you linked to in post #4 show (page 15) that the class constructor needs to be called with the applications context, but you are calling it with no parameters at all (empty constructor).
Try
B4X:
Dim context As JavaObject
context.InitializeContext
Dim M as JavaObject
M = NiOBJ.InitializeNewInstance("npi.sdk.NPrinterLib", Array As Object(context))

Yeah, it works!
Thanks a Lot!

e.g.: how can I set this problem "[SOLVED]"
 
Upvote 0
Top