Skip to content
Advertisement

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. Therefore I used this custom method in java in a kivy App using pyjnius for the same reason as stated in the link.

I placed the ReadInput.java file in this directory:
~/Build_Environ/.buildozer/android/platform/build/dists/JniusPrintBluetoothAppie/src/main/java/org/kivy/android

I initialised the java class with pyjnius:

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

The java code:

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:

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:

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?

Advertisement

Answer

The reason is that in Python code you access the method in a static way:

Reading.inputread(...)

This will work only if you define the method inputread in Java as 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:

Reading = autoclass('org.kivy.android.ReadInput')
reading = Reading()
reading.inputread(...)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement