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

Discussion in 'Android Questions' started by asales, Jun 13, 2018.

  1. asales

    asales Well-Known Member Licensed 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.

    Code:
    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, 
    00300dip300dip)
        
    Dim lbl As Label
        lbl.Initialize(
    "")
        lbl.Text = NativeContentAd.RunMethod(
    "getHeadline"Null)
        content.AddView(lbl, 
    10dip10dip300dip50dip)
        NativeContentAdView.RunMethod(
    "setHeadlineView"Array(lbl))
        
    Dim lbl2 As Label
        lbl2.Initialize(
    "")
        lbl2.Text = NativeContentAd.RunMethod(
    "getBody"Null)
        content.AddView(lbl2, 
    10dip60dip300dip50dip)
        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, 
    200dip0100dip100dip)
           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, 
    200dip100dip100dip100dip)
           NativeContentAdView.RunMethod(
    "setImageView"Array(imgView))
        
    End If
        NativeContentAdView.RunMethod(
    "setNativeAd"Array(NativeContentAd))
       
       
    Activity.AddView(pNativeAdView, 00100%x200dip)
    End Sub
    Code:
    *** 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)
     
    Multiverse app likes this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Works fine here:
    Code:
    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.
     
    asales and AnandGupta like this.
  3. Multiverse app

    Multiverse app Active Member Licensed User

    None of the native ads are available in our admob. Is it in beta?
     
  4. asales

    asales Well-Known Member Licensed User

    I checked with a Google Manager and the ads are still not to all developers.
    He told me the banner 300x250 get a more revenue then a normal banner. I'll check.
     
  5. asales

    asales Well-Known Member Licensed User

    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.
     
    Douglas Farias likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice