Android Question call Sub From in Line java to B4a Class Module

david7374

Member
Hi everyone
in activity, it's easy to call sub from in Line java to b4a, for example
[CODE lang="b4x" title="call sub from java to b4a in activity"]Sub Activity_Create(FirstTime As Boolean)

Private jo As JavaObject
jo.InitializeContext
jo.RunMethod("startPreview1",Null)

End Sub

Private Sub send_qv(s As String)
Log(s)
End Sub

#if java

public void startPreview1() {
subQvEvent("send","Mohammad");
}

public void subQvEvent(String EventName,String values) {

processBA.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_qv", values);

}

#End If
[/CODE]


but in b4aClass, I do not understand where the error comes from, here is my code
[CODE lang="b4x" title="call sub from in Line java to b4a in b4aClass Module"]Public Sub Initialize

Private jo As JavaObject
jo.InitializeNewInstance(Application.PackageName&".check.thisQv", Array(Null))
jo.RunMethod("startPreview1",Null)

End Sub

Private Sub send_qv(s As String)
Log(s)
End Sub


#if java

public class thisQv{

import anywheresoftware.b4a.BA;

public BA processBA;
public BA ba;


public void startPreview1() {
subQvEvent("send","Mohammad");
}

public void subQvEvent(String EventName,String values) {

processBA.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_qv", values);

}

}
#End If[/CODE]

and this error :
[CODE lang="b4x" title="error"]Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.BA.raiseEventFromUI(java.lang.Object, java.lang.String, java.lang.Object[])' on a null object reference
[/CODE]


These are other codes that I tried and unfortunately got the same error.
[CODE lang="b4x" title="call sub from java to b4a in b4aClass"]
processBA.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_qv", values);

ba.raiseEvent(this,EventName.toLowerCase(BA.cul) + "_qv", values);

ba.raiseEvent(ba,EventName.toLowerCase(BA.cul) + "_qv", values);

ba.raiseEvent2(null, true, EventName.toLowerCase(BA.cul) + "_qv",false,values);[/CODE]



My main question is that, How call Sub From in Line java to B4a Class Module.
Thanks.
 
Last edited:
Thanks Erel.
In fact, with your help, I learned how to call a function from Java to B4XPages.
But my main problem still persists. How to do it in standard class?
I use classes to categorize code as well as more scalability and readability.
Sometimes I need to use Java functions in classes. These functions may give us the output function after doing a series of tasks, which I need to use these values of this output function in b4a.
For example, when downloading a file, we have two functions, one is the download progress, the values of which are specified when downloading, and the other is called when the download is complete.
[CODE lang="b4x" title="Progress Download and Complete"]#if java

protected void onProgressUpdate(String... progress) {
// setting progress percentage
}

protected void onProgressComplete() {

}

#End if[/CODE]

If we want to use functions, we have to refer the values of these functions to b4a.
In B4XPages Class and activities it's ok. but standard class?
I have not yet found a solution to this issue in the standard class.
 
Upvote 0
B4XPages are standard class modules.

Thanks yeah, it was solved. I did not pay attention to this point at all,
Yes, exactly the problem was solved.
Thankful.

Here is my code:
[CODE lang="b4x" title="call sub from in line java to b4a in b4aClass"]Public Sub Initialize


Me.As(JavaObject).RunMethod("startPreview1", Null)

End Sub

Private Sub send_qv(s As String)
Log(s)
End Sub


#if java


public void startPreview1() {
subQvEvent("send","Mohammad");
}

public void subQvEvent(String EventName,String values) {

getBA().raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_qv", values);

}

#End If
[/CODE]
 
Upvote 0
Back
Top