Android Question [Solved] B4XPages - How to call a sub in Main from a third b4xpage

asales

Well-Known Member
Licensed User
I'm trying to use the Admob Native Ad in B4XPages.

I the Main module I have the sub thats show if the native ad added in B4XMainPages was received:
B4X:
(MAIN):
Sub NativeAd_ReceiveAd
    B4XPages.MainPage.NativeAd_ReceiveAd
End Sub

#if Java
public static class MyAdListener extends com.google.android.gms.ads.AdListener {   
   String eventName;
   public MyAdListener(String s) {
       eventName = s.toLowerCase(BA.cul);
   }
  
   public void onAdLoaded() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_receivead", false, null);
   }   
}
#End If

(B4XMAINPAGE):
Sub NativeAd_ReceiveAd
    Log("Ad1_ReceiveAd")
End Sub

Now I want to put a native ad in a new b4xpage (B4XPage_Native):
B4X:
(B4XPAGE_NATIVE):
Sub NativeAd2_ReceiveAd
    Log("Ad2_ReceiveAd")
End Sub

How I can check if the ad2 was received in this new page?

Thanks in advance for any tip.
 

asales

Well-Known Member
Licensed User
Why are you using Main instead of B4XMainPage?
Because this part (.main$MyAdListener) of the native ad code, that call the Java code:
B4X:
Dim Listener As JavaObject
Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array(AdName))  'change 'main' with the current activity module name
builder.RunMethod("withAdListener", Array(Listener))
I'm trying to finish this code (to put here in the forum) to use it with B4XPages, and show the events of the native ad.:
 
Upvote 0

asales

Well-Known Member
Licensed User
Put the Java code in B4XMainPage and change .main with .b4xmainpage

If you get any error then post the code and error message. I will help you fix it.
Here the code of the sub LoadNativeAd (in B4XMainPage - and I'll want to use it in other pages):
B4X:
Sub LoadNativeAd(AdName As String, IdAd As String)
    Dim AdUnitId As String = IdAd
 
    Dim ctxt As JavaObject
    ctxt.InitializeContext

    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))

    Dim onUnifiedAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/nativead/NativeAd.OnNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedAdLoaded", Null)
    builder.RunMethod("forNativeAd", Array(onUnifiedAdLoadedListener))

    Dim Listener As JavaObject
    Listener.InitializeNewInstance(Application.PackageName & ".b4xmainpage$MyAdListener", Array(AdName))
    builder.RunMethod("withAdListener", Array(Listener))
Here the Java code (in B4XMainPage):
B4X:
#if Java
public static class MyAdListener extends com.google.android.gms.ads.AdListener {
   String eventName;
   public MyAdListener(String s) {
       eventName = s.toLowerCase(BA.cul);
   }

   public void onAdClosed() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adclosed", false, null);
   }

   public void onAdFailedToLoad(int arg0) {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_failedtoreceivead", false, new Object[] {String.valueOf(arg0)});
   }

   public void onAdLeftApplication() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adleftapplication", false, null);
   }

   public void onAdLoaded() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_receivead", false, null);
   }

   public void onAdClicked() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_clicked", false, null);
   }
}
#End If
Here the error when I tried to compile:
B4X:
Compiling resources    (0.17s)
Linking resources    (0.78s)
Compilando o código Java.    Error
src\br\com\as2\testes\b4xmainpage.java:409: error: cannot find symbol
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adclosed", false, null);
       ^
  symbol:   variable processBA
  location: class MyAdListener
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

javac 11.0.1
Here the error if I remove the Java code (compile without errors):
B4X:
b4xmainpage$ResumableSub_LoadNativeAdresume (java line: 131)
java.lang.ClassNotFoundException: br.com.apps$testes$$b4xmainpage$MyAdListener
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:289)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:84)
    at br.com.apps.testes.b4xmainpage$ResumableSub_LoadNativeAd.resume(b4xmainpage.java:131)
    at br.com.apps.testes.b4xmainpage._vvvv3(b4xmainpage.java:68)
    at br.com.apps.testes.b4xmainpage._b4xpage_appear(b4xmainpage.java:38)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6123)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

If I put the Java code in the Main module and change this line, it works:
B4X:
Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array(AdName))

But I don't know how I can use it to show the native ad in other pages and call the events: loaded, closed, failed...

Thanks for your support.
 
Last edited:
Upvote 0

asales

Well-Known Member
Licensed User
Create a small project and upload it.
Here!
To the code in B4XMainPage I can see the events: loaded, closed, failed...
How I can see the events to the native ad in the b4xpage B4xNative2 too?
 

Attachments

  • ProjectNativeAd.zip
    17.7 KB · Views: 31
Upvote 0

Erel

Administrator
Staff member
Licensed User
1.
B4X:
Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array(AdName, Me))

2. Keep the java code in the Main module and change it to:
B4X:
#if Java
import anywheresoftware.b4a.B4AClass;
public static class MyAdListener extends com.google.android.gms.ads.AdListener {  
   String eventName;
   B4AClass target;
   public MyAdListener(String s, B4AClass target) {
       eventName = s.toLowerCase(BA.cul);
       this.target = target;
   }
  
   public void onAdClosed() {
       target.getBA().raiseEventFromDifferentThread(null, null, 0, eventName + "_adclosed", false, null);
   }
  
   public void onAdFailedToLoad(int arg0) {
        target.getBA().raiseEventFromDifferentThread(null, null, 0, eventName + "_failedtoreceivead", false, new Object[] {String.valueOf(arg0)});
   }
  
   public void onAdLeftApplication() {
        target.getBA().raiseEventFromDifferentThread(null, null, 0, eventName + "_adleftapplication", false, null);
   }
  
   public void onAdLoaded() {
        target.getBA().raiseEventFromDifferentThread(null, null, 0, eventName + "_receivead", false, null);
   }  
  
   public void onAdClicked() {
        target.getBA().raiseEventFromDifferentThread(null, null, 0, eventName + "_clicked", false, null);
   }
}
#End If
 
Upvote 0
Top