Android Question Inline Java - Constructor not found

Soheyl

Member
Licensed User
Longtime User
Hello everyone,
I would like to use OutputStream. I tested this code and it works in B4J but not in B4A.

Below is the inline Java code where I encountered the error message "Constructor not found":

B4X:
Dim out As JavaObject
    out.InitializeNewInstance($"${Application.PackageName}.main$MyOutputStream"$, Array(Me))

#If Java
import java.io.*;
import anywheresoftware.b4a.B4AClass;
public static class MyOutputStream extends OutputStream {
    B4AClass cls;
    private boolean closed;
    public MyOutputStream (B4AClass cls) {
        this.cls = cls;
    }
   
    public void write(int b) throws IOException {
        if (closed)
            throw new IOException("closed");
        cls.getBA().raiseEventFromDifferentThread (null, null, 0, "data_available", true, new Object[] {new byte[] {(byte)b}});
    }
    public void write(byte b[], int off, int len) throws IOException {
        if (closed)
            throw new IOException("closed");
        byte[] bb = new byte[len];
        System.arraycopy(b, off, bb, 0, len);
        cls.getBA().raiseEventFromDifferentThread (null, null, 0, "data_available", true, new Object[] {bb});
    }
    public void close() {
        closed = true;
    }
}
#End if


but in B4A, I encountered the following log:

Log
B4X:
starter$ResumableSub_httpClient_ResponseSuccessresume (java line: 235)
java.lang.RuntimeException: Constructor not found.
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:95)
    at com.example.exa.starter$ResumableSub_httpClient_ResponseSuccess.resume(starter.java:235)
    at com.example.exa.starter._httpclient_responsesuccess(starter.java:205)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7884)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
starter$ResumableSub_httpClient_ResponseSuccessresume (java line: 235)
java.lang.RuntimeException: Constructor not found.
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:95)
    at com.example.exa.starter$ResumableSub_httpClient_ResponseSuccess.resume(starter.java:235)
    at com.example.exa.starter._httpclient_responsesuccess(starter.java:205)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7884)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Just to let you know, I have added the following line for the B4A version:
B4X:
import anywheresoftware.b4a.B4AClass;

Could someone please take a look at it?

thank you
 
Solution
It was because of the Service.
I think we cannot pass a service; the mistake I made was sending a class instead.
I created a new class and passed that instead of "Me" and it worked.

Somthing like this:
B4X:
Dim testClass As EmptyClass
    testClass.Initialize
Dim out As JavaObject
    out.InitializeNewInstance($"${Application.PackageName}.main$MyStream"$, Array(testClass))

Thank you.

DonManfred

Expert
Licensed User
Longtime User
What exactly are you trying to archieve?
 
Upvote 0

Soheyl

Member
Licensed User
Longtime User
I want to use OkHttpRequest to send a POST request, and the response is a Server Side Event. So, I will use this inline part to raise an event for incoming new data.

Would you like a sample project?
 
Upvote 0

Soheyl

Member
Licensed User
Longtime User
It was because of the Service.
I think we cannot pass a service; the mistake I made was sending a class instead.
I created a new class and passed that instead of "Me" and it worked.

Somthing like this:
B4X:
Dim testClass As EmptyClass
    testClass.Initialize
Dim out As JavaObject
    out.InitializeNewInstance($"${Application.PackageName}.main$MyStream"$, Array(testClass))

Thank you.
 
Upvote 0
Solution
Top