Italian Problema con raiseEvent in una Custom Java Library

Stolcius Von Stolcenberg

Member
Licensed User
Sto tentando di triggare su B4A un ba.raiseEvent implementato all'interno di una lib Java compilata con Simple Library Compiler come SampleLib.


B4X:
package anywheresoftware.b4a.objects;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.CompoundButton.OnCheckedChangeListener;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.keywords.Common.DesignerCustomView;
import anywheresoftware.b4a.keywords.constants.Colors;
import anywheresoftware.b4a.keywords.Common;

@ActivityObject
@ShortName("SampleLib")
@Events(values={"MyEvent_Fire(Value As String)"})
public class SampleLib {
    private BA ba;
    private String eventName="MyEvent";
    /**
     * Multiple the given by two
     *
     */
     public void testRaiseEvent(String Value) {
            Common.Log("inside testRaiseEvent ");
            ba.raiseEvent(this, eventName.toLowerCase(BA.cul) + "_fire", new Object[] {Value});
    }
        
}

In B4A ho linkato SampleLib e implementato il seguente codice.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim sl As  SampleLib
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    sl.testRaiseEvent("test passed!")
   
End Sub
Private Sub MyEvent_Fire(Value As String)
    Log($"Fire: ${Value}"$)
   
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Ricevo il seguente errore (LOG):

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
inside testRaiseEvent
Error occurred on line: 31 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4a.BA.raiseEvent(java.lang.Object, java.lang.String, java.lang.Object[])' on a null object reference
    at anywheresoftware.b4a.objects.SampleLib.testRaiseEvent(SampleLib.java:26)
    at b4a.example.main._activity_create(main.java:375)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at b4a.example.main.afterFirstLayout(main.java:102)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
** Activity (main) Resume **

Dove sbaglio?

TIA per l'aiuto,
Stolcius
 

MarcoRome

Expert
Licensed User
Longtime User
B4X:
package anywheresoftware.b4a.objects;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.CompoundButton.OnCheckedChangeListener;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.keywords.Common.DesignerCustomView;
import anywheresoftware.b4a.keywords.constants.Colors;
import anywheresoftware.b4a.keywords.Common;

@ActivityObject
@ShortName("test")
@Events(values={"Fire(Value As String)"})
public class test {
    private BA ba;
    private String eventName;
    /**
     * Multiple the given by two
     *
     */
     public void initialize(BA ba, String EventName)      
{
          this.ba = ba;
          this.eventName = EventName.toLowerCase(BA.cul);
}
    
    
     public void testRaiseEvent(String Value) {
            Common.Log("inside testRaiseEvent ");
             if (ba.subExists(eventName + "_fire")) {
                    BA.Log("lib:Raising.. "+eventName+ "_fire " + Value);                               
                    ba.raiseEventFromDifferentThread(this, null, 0,eventName + "_fire" , false, new Object[] {Value});
              }else {
                  BA.Log("lib: NOTFOUND '"+eventName + "_fire");
              }
           
       
}
}

B4A

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim sl As  test
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
  
       sl.initialize("sl")
    sl.testRaiseEvent("test passed!")
  
End Sub

Sub sl_Fire(Value As String)
    Log($"Fire: ${Value}"$)
End Sub
 

Stolcius Von Stolcenberg

Member
Licensed User
Grazie, Marco,
Solved: funzica tutto alla grande! :)

Avevo compulsato i forum della community senza riuscire a cavare un proverbiale ragno dal cyber-buco.

Un salutone,
Stolcius

----
Per aspera ad dis-astra
 
Top