简体   繁体   English

Pyjnius 自定义 Java 方法返回“JavaException:无法找到无方法”在公共静态之后有效

[英]Pyjnius custom java method returning 'JavaException: Unable to find a None Method' works after Public Static

So I needed to read a ByteArray from the InputStream in Android.所以我需要从 Android 的 InputStream 中读取一个 ByteArray。 Therefore I used this custom method in java in a kivy App using pyjnius for the same reason as stated in the link.因此,出于与链接中所述相同的原因,我在使用 pyjnius 的 kivy 应用程序中的 java 中使用了此自定义方法

I placed the ReadInput.java file in this directory:我将 ReadInput.java 文件放在这个目录中:
~/Build_Environ/.buildozer/android/platform/build/dists/JniusPrintBluetoothAppie/src/main/java/org/kivy/android

I initialised the java class with pyjnius:我用 pyjnius 初始化了 java 类:

Reading = autoclass('org.kivy.android.ReadInput')

The java code: Java代码:

package org.kivy.android;

import java.io.InputStream;
import java.lang.Byte;
import java.lang.Integer;
import java.io.IOException;

public class ReadInput {
    public byte[] inputread(InputStream stream, int count) throws IOException {
        byte[] by = new byte[count];
        stream.read(by);
        return by;
    }
}


I read from the buffer in python using the following code:我使用以下代码从 python 中的缓冲区读取:

Reading.inputread(self.recv_stream, 4) #recv_stream is an Android BluetoothAdapter createInsecureRfcommSocketToServiceRecord getInputStream object

But for some reason this above code constantly gave me the following error:但出于某种原因,上面的代码不断给我以下错误:
JavaException: Unable to find a None Method

After many, many days of struggle I finally got the method to work by simply declaring the method as: public static The new java method looked as follows and I called it in the same way as above:经过很多天的努力,我终于通过简单地将方法声明为: public static新的java方法如下所示,我以与上面相同的方式调用它:

package org.kivy.android;

import java.io.InputStream;
import java.lang.Byte;
import java.lang.Integer;
import java.io.IOException;

public class ReadInput {
    public static byte[] inputread(InputStream stream, int count) throws IOException {
        byte[] by = new byte[count];
        stream.read(by);
        return by;
    }
}



What I want to know is why would the word 'static' make the java method suddenly work?我想知道的是为什么“静态”这个词会让java方法突然起作用?

The reason is that in Python code you access the method in a static way:原因是在 Python 代码中,您以静态方式访问该方法:

Reading.inputread(...)

This will work only if you define the method inputread in Java as static .仅当您将 Java 中的inputread方法定义为static 时,这才有效

But it was not necessary.但这没有必要。 It could better to keep the method as non-static in Java and to use it in Python in a normal non-static way:最好在 Java 中将该方法保持为非静态,并以正常的非静态方式在 Python 中使用它:

Reading = autoclass('org.kivy.android.ReadInput')
reading = Reading()
reading.inputread(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM