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
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


but in b4aClass, I do not understand where the error comes from, here is my code
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

and this error :
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


These are other codes that I tried and unfortunately got the same error.
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);



My main question is that, How call Sub From in Line java to B4a Class Module.
Thanks.
 
Last edited:

david7374

Member
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.
Progress Download and Complete:
#if java

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

protected void onProgressComplete() {
            
 }

#End if

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

david7374

Member
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:
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
 
Upvote 0
Top