【问题标题】:How to pass android context into a java constructor class using pyjnius?如何使用pyjnius将android上下文传递给java构造函数类?
【发布时间】:2020-09-21 14:34:04
【问题描述】:

我正在使用 kivy 为具有内置热敏打印机的 android 设备构建应用程序。我已经找到了可以让我访问打印机的 API,并且是用 java 编写的(所以我使用 pyjnius),但我无法让它工作。我在一个名为“JarExplorer”的程序中看到,我感兴趣的构造函数类需要一个名为“android.content.context Context”的参数,但我不知道如何正确传递它.

这是我尝试过的python代码:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from jnius import autoclass

# Java Context class
Context = autoclass('android.content.Context')

# Java ThermalPrinter class
Printer = autoclass('com.telpo.tps550.api.printer.UsbThermalPrinter')

class Box(BoxLayout):
    def construct_method1(self):
        self.Printer = Printer()

    def construct_method2(self):
        self.Printer = Printer('android/content/Context')

    def construct_method3(self):
        self.Printer = Printer(Context())

    def print_something(self):
        # Open the printer
        self.Printer.start()
        # Specifying text
        self.Printer.addString(self.ids.input.text)
        # Print text
        self.Printer.printString()
        # Release the printer
        self.Printer.stop()

class MainApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

MainApp().run()

使用 方法 1,logcat 确认我需要传递 android 上下文:

06-02 20:41:29.688 28544 28925 I python  :    File "/home/ivo/Escritorio/Printer/.buildozer/android/app/main.py", line 14, in construct_method1
06-02 20:41:29.689 28544 28925 I python  :    File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.__init__
06-02 20:41:29.690 28544 28925 I python  :    File "jnius/jnius_export_class.pxi", line 294, in jnius.jnius.JavaClass.call_constructor
06-02 20:41:29.691 28544 28925 I python  :  jnius.jnius.JavaException: Invalid call, number of argument mismatch for constructor, available: ['(Landroid/content/Context;)V']
06-02 20:41:29.691 28544 28925 I python  : Python for android ended.

使用方法2的Logcat:

06-02 20:44:33.109 29084 29126 I python  :    File "jnius/jnius_export_class.pxi", line 256, in jnius.jnius.JavaClass.__init__
06-02 20:44:33.110 29084 29126 I python  :    File "jnius/jnius_export_class.pxi", line 326, in jnius.jnius.JavaClass.call_constructor
06-02 20:44:33.111 29084 29126 I python  :    File "jnius/jnius_conversion.pxi", line 109, in jnius.jnius.populate_args
06-02 20:44:33.112 29084 29126 I python  :  jnius.jnius.JavaException: Invalid python object for this argument. Want 'android/content/Context', got 'android/content/Context'
06-02 20:44:33.112 29084 29126 I python  : Python for android ended.

方法3的Logcat:

06-02 20:47:24.241 30838 30876 F rg.test.printe: java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: can't make objects of type android.content.Context: 0x6fdea688

然后应用关闭。

我知道 API 应该可以工作,因为它带有一个可以成功打印但完全用 Java 编写的演示应用程序。

你们知道什么是错的还是更深层次的问题?我非常感谢您的帮助和 知识:) 谢谢!

【问题讨论】:

    标签: java python android kivy pyjnius


    【解决方案1】:

    我在使用 android CameraManager 时遇到了类似的问题。它需要应用程序上下文作为参数。适用于此的最小示例如下:

    from jnius import autoclass
    
    CameraManager = autoclass('android.hardware.camera2.CameraManager')
    Application = autoclass('android.app.Application')
    
    
    def android_camera2_test():
        print(Application.getProcessName())
        app = Application()
        cm = CameraManager(app)
        print(cm.getCameraIdList())
    

    因为 Application 类是从 Context 类派生的,所以这行得通。我不太“流利”以正确的方式在 android 中正确处理 Context/Application 变量,但它适用于初始测试。

    因此,对于您的测试,我建议添加/更改以下几行:

    ...
    
    Application = autoclass('android.app.Application')
    
    ...
    
    class Box(BoxLayout):
        app = Application()
    
        ...
    
        def construct_method2(self):
            self.Printer = Printer(self.app)
    
        ...
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2022-08-14
      • 2020-04-02
      • 1970-01-01
      • 2015-12-18
      • 2021-10-23
      • 2018-10-01
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多