Android Question Error Context in Java inline Class module

Lucas Siqueira

Active Member
Licensed User
Longtime User
Can anyone help me implement the java code that works in the Main module. I want to implement it in a class called Print.

But when I try to use it in the Print class, it gives an error in the JAVA code:
error: incompatible types: print cannot be converted to Context
printer = Printer.getInstance(this);


I've tried several ways to run the code in the class, and there's always an error.





Main:
#Region  Project Attributes
    #ApplicationLabel: maquininhaLite
    #VersionCode: 1
    #VersionName: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
    #BridgeLogger: True
#End Region

#AdditionalJar: positivo-printer-1.00.00.aar
#AdditionalJar: posmpapi_1.01.01.aar

Sub Process_Globals
    Private xui As XUI
    Private NativeMe As JavaObject
    Private Print1 As print
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        NativeMe.InitializeContext
    End If
    Print1.Initialize
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Private Sub Activity_Click
    'work
    imprimirTexto("Teste Activity")
    
    'not work
    Print1.imprimirTexto("Teste Class")
End Sub

Public Sub imprimirTexto(Texto1 As String)
    NativeMe.RunMethod("imprimirTexto", Array As String(Texto1))
End Sub

'Inline Java Code -> https://www.b4x.com/android/forum/threads/inline-java-code.50141
#IF JAVA
//biblioteca immpressora
import br.com.execucao.posmp_api.printer.Printer;
import br.com.execucao.posmp_api.printer.PrinterListener;

private Printer printer;

public void imprimirTexto(String texto1) {
    printer.print(texto1);
}

public void _onCreate() {   
     printer = Printer.getInstance(this);
     printer.open();     
     printer.setFeedLineAuto(true);
}

public void _onDestroy() {
    printer.close();
}
#End If



Class Print:
Private Sub Class_Globals
    Private NativeMe As JavaObject
End Sub

Public Sub Initialize
    NativeMe.InitializeContext
End Sub

Public Sub imprimirTexto(Texto1 As String)
    NativeMe.RunMethod("imprimirTexto", Array As String(Texto1))
End Sub

'Inline Java Code -> https://www.b4x.com/android/forum/threads/inline-java-code.50141
#IF JAVA
//biblioteca immpressora
import br.com.execucao.posmp_api.printer.Printer;
import br.com.execucao.posmp_api.printer.PrinterListener;

private Printer printer;

public void imprimirTexto(String texto1) {
    printer.print(texto1);
}

public void _onCreate() {   
     //printer = Printer.getInstance(this);
     printer.open();     
     printer.setFeedLineAuto(true);
}

public void _onDestroy() {
    printer.close();
}
#End If




Erro:
B4A Versão: 12.50
Analisando o código.    (0.01s)
    Java Versão: 14
Building folders structure.    (0.02s)
Compilando o código    (0.03s)
Compilando código dos layouts.    (0.00s)
Organizando bibliotecas    (0.00s)
    (AndroidX SDK)
Compilando recursos    (0.08s)
Linkando recursos    (0.33s)
Compilando o código Java.    Error
src\b4a\example\print.java:68: error: incompatible types: print cannot be converted to Context
     printer = Printer.getInstance(this);
                                   ^
Note: src\b4a\example\main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

javac 14.0.1
 

Attachments

  • maquininhaLite.zip
    204.3 KB · Views: 138
Solution
working

Class Print:
Private Sub Class_Globals
End Sub

Public Sub Initialize
End Sub

Public Sub imprimirTexto(Texto1 As String)   
    Dim printer As JavaObject
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    printer.InitializeStatic("br.com.execucao.posmp_api.printer.Printer")
    printer = printer.RunMethod("getInstance", Array(ctxt))
    printer.RunMethod("open", Null)
    printer.RunMethod("setFeedLineAuto", Array(True))
    printer.RunMethod("print", Array(Texto1))
End Sub

Lucas Siqueira

Active Member
Licensed User
Longtime User
1. The "hooks" you added will only work in an activity.
2. You need to use JavaObject.InitializeContext to get the context object.
3. Using activities is a waste of time. Switch to B4XPages.
4. I recommend removing this java code and implementing it with JavaObject. It will be simpler.
My project is old and quite large, I can't modify it for b4xpages at the moment. This code I published is just a piece to ask for help.

How could I implement JavaObject? Could you give me an example in my code?
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
working

Class Print:
Private Sub Class_Globals
End Sub

Public Sub Initialize
End Sub

Public Sub imprimirTexto(Texto1 As String)   
    Dim printer As JavaObject
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    printer.InitializeStatic("br.com.execucao.posmp_api.printer.Printer")
    printer = printer.RunMethod("getInstance", Array(ctxt))
    printer.RunMethod("open", Null)
    printer.RunMethod("setFeedLineAuto", Array(True))
    printer.RunMethod("print", Array(Texto1))
End Sub
 
Upvote 0
Solution
Top