Android Question [Solved] Example to the new Unified Native Ad

asales

Expert
Licensed User
Longtime User
I tried to modify this example:
https://www.b4x.com/android/forum/threads/any-news-about-native-ads-advanced.88100/#post-562893

to show the new Unified Native Ad:
https://developers.google.com/android/reference/com/google/android/gms/ads/formats/UnifiedNativeAd

This is my modified code (the changes are marked with "***code change"), but I get the error below the code:
java.lang.ClassNotFoundException: com.google.android.gms.ads$formats$UnifiedNativeAd$OnUnifiedNativeAdLoadedListener

Any tip to solved this problem are welcome.

Thanks in advance.

B4X:
Sub LoadUnifiedNativeAd
       Dim AdUnitId As String = "ca-app-pub-3940256099942544/2247696110"
   
       Dim ctxt As JavaObject
       ctxt.InitializeContext
   
       Dim builder As JavaObject
       builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
   
   '*** Code changed
   Dim onUnifiedAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/UnifiedNativeAd.OnUnifiedNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedNativeAdLoaded", Null)
   builder.RunMethod("forUnifiedNativeAd", Array(onUnifiedAdLoadedListener))
   '*** End Code changed
   
       Dim Listener As JavaObject
       Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array("UnifiedNativeAd"))  'change 'main' with the current activity module name
       builder.RunMethod("withAdListener", Array(Listener))
   
       Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
       Dim AdRequestBuilder As JavaObject
       AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
       AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
   
   '*** Code changed
   Wait For (builder) UnifiedAdLoaded_Event (MethodName As String, Args() As Object)
   '*** End Code changed
   
   Dim NativeContentAd As JavaObject = Args(0)

       Dim NativeContentAdView As JavaObject
   '*** Code changed
   NativeContentAdView.InitializeNewInstance("com/google/android/gms/ads/formats/UnifiedNativeAdView".Replace("/", "."), _
       Array(ctxt))
   '*** End Code changed
     
    Dim pNativeAdView As Panel = NativeContentAdView
   
    Dim content As Panel
    content.Initialize("")
    pNativeAdView.AddView(content, 0, 0, 300dip, 300dip)
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = NativeContentAd.RunMethod("getHeadline", Null)
    content.AddView(lbl, 10dip, 10dip, 300dip, 50dip)
    NativeContentAdView.RunMethod("setHeadlineView", Array(lbl))
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.Text = NativeContentAd.RunMethod("getBody", Null)
    content.AddView(lbl2, 10dip, 60dip, 300dip, 50dip)
    NativeContentAdView.RunMethod("setBodyView", Array(lbl2))
    Dim logo As JavaObject = NativeContentAd.RunMethod("getLogo", Null)
    If logo.IsInitialized Then
       Dim logoView As Panel
       logoView.Initialize("")
       logoView.Background = logo.RunMethod("getDrawable", Null)
       content.AddView(logoView, 200dip, 0, 100dip, 100dip)
       NativeContentAdView.RunMethod("setLogoView", Array(logoView))
    End If
    Dim images As List = NativeContentAd.RunMethod("getImages", Null)
    If images.IsInitialized And images.Size > 0 Then
       Dim imgView As Panel
       imgView.Initialize("")
       Dim image As JavaObject = images.Get(0)
       imgView.Background = image.RunMethod("getDrawable", Null)
       content.AddView(imgView, 200dip, 100dip, 100dip, 100dip)
       NativeContentAdView.RunMethod("setImageView", Array(imgView))
    End If
    NativeContentAdView.RunMethod("setNativeAd", Array(NativeContentAd))
   
   Activity.AddView(pNativeAdView, 0, 0, 100%x, 200dip)
End Sub

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main$ResumableSub_LoadUnifiedNativeAdresume (java line: 847)
java.lang.ClassNotFoundException: com.google.android.gms.ads$formats$UnifiedNativeAd$OnUnifiedNativeAdLoadedListener
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:288)
    at anywheresoftware.b4j.object.JavaObject.createEvent(JavaObject.java:252)
    at anywheresoftware.b4j.object.JavaObject.CreateEventFromUI(JavaObject.java:222)
    at br.com.as2.testes.main$ResumableSub_LoadUnifiedNativeAd.resume(main.java:847)
    at br.com.as2.testes.main._v5(main.java:797)
    at br.com.as2.testes.main._activity_create(main.java:347)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at br.com.as2.testes.main.afterFirstLayout(main.java:104)
    at br.com.as2.testes.main.access$000(main.java:17)
    at br.com.as2.testes.main$WaitForLayout.run(main.java:82)
    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)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Works fine here:
B4X:
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/formats/UnifiedNativeAd.OnUnifiedNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedNativeAdLoaded", Null)

Open B4A Sdk manager and make sure that all recommended items are updated.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Works fine here:
B4X:
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/formats/UnifiedNativeAd.OnUnifiedNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedNativeAdLoaded", Null)

Open B4A Sdk manager and make sure that all recommended items are updated.
It works. Thanks!
I made a few changes in the code and later I'll put the codes of the natives ads in the Code Snippets section.
 
Upvote 0
Top