0

I am using below java code :

public class MainActivity extends AppCompatActivity {


    private String unityGameID = "******";
    private Boolean testMode = false;
    private String placementIdbanner = "baner";
    private View bannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Declare a new banner listener, and set it as the active banner listener:
        final IUnityBannerListener myBannerListener = new UnityBannerListener ();
        UnityBanners.setBannerListener (myBannerListener);
        // Initialize the Ads SDK:
        UnityAds.initialize (this, unityGameID, (IUnityAdsListener) myBannerListener, testMode);

    }

    public void ToggleBannerAd () {
        // If no banner exists, show one; otherwise remove the existing one:
        if (bannerView == null) {
            // Optionally specify the banner’s anchor position:
            UnityBanners.setBannerPosition(BannerPosition.BOTTOM_CENTER);
            // Request ad content for your Placement, and load the banner:
            UnityBanners.loadBanner(MainActivity.this, placementIdbanner);
        } else {
            UnityBanners.destroy();
        }
    }

    // Implement the banner listener interface methods:
    private class UnityBannerListener implements IUnityBannerListener {

        @Override
        public void onUnityBannerLoaded (String placementId, View view) {
            // When the banner content loads, add it to the view hierarchy:
            bannerView = view;
            //((ViewGroup) findViewById (R.id.unityads_example_layout_root)).addView (view);
        }

        @Override
        public void onUnityBannerUnloaded (String placementId) {
            // When the banner’s no longer in use, remove it from the view hierarchy:
            bannerView = null;
        }

        @Override
        public void onUnityBannerShow (String placementId) {
            // Called when the banner is first visible to the user.
        }

        @Override
        public void onUnityBannerClick (String placementId) {
            // Called when the banner is clicked.
        }

        @Override
        public void onUnityBannerHide (String placementId) {
            // Called when the banner is hidden from the user.
        }

        @Override
        public void onUnityBannerError (String message) {
            // Called when an error occurred, and the banner failed to load or show.
        }
    }

}

I get these errors in my logcat as mentioned below :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.firstapp, PID: 9228
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstapp/com.example.firstapp.MainActivity}: java.lang.ClassCastException: com.example.firstapp.MainActivity$UnityBannerListener cannot be cast to com.unity3d.ads.IUnityAdsListener
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassCastException: com.example.firstapp.MainActivity$UnityBannerListener cannot be cast to com.unity3d.ads.IUnityAdsListener
        at com.example.firstapp.MainActivity.onCreate(MainActivity.java:102)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

Above is my code and logcat output from my android application.

I followed this documentation in my app https://unityads.unity3d.com/help/android/integration-guide-android#banner-ads

Please check this document and guide me how to implement this banner ad from UnityAds.

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
Ak23
  • 342
  • 1
  • 5
  • 17

2 Answers2

0

You forgot to implement IUnityAdsListener at the top :

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IUnityAdsListener
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
  • Not work same error. I think not this problem because I already implemet in below class UnityBannerListener. – Ak23 Dec 26 '19 at 06:57
  • But in the tutorial you provided it has implemented it like this where it is initializing the sdk ; Do this in that class (InitializeAdsScript) : public class InitializeAdsScript extends AppCompatActivity implements View.OnClickListener, IUnityAdsListener { – intellignt_idiot Dec 26 '19 at 07:06
  • Ok I see that in the documentation also it is not implemented. But it is written. So implement it and then try : IN the documentation it is telling to implement this :IUnityBannerListener In your game script, import the UnityBanners API, then implement an IUnityBannerListener interface to provide callbacks to the SDK. Use the loadBanner and destroy functions to show or hide the banner. The following script sample is an example implementation for displaying banner ads: – intellignt_idiot Dec 26 '19 at 07:11
  • This line of error : Caused by: java.lang.ClassCastException: com.example.firstapp.MainActivity$UnityBannerListener cannot be cast to com.unity3d.ads.IUnityAdsListener It means your MainActivity cannot be cast to IUnityAdsListener. Because it is of type Activity. Now if you implement MainActivity implements IUnityAdsListener , then this error will not come. Tell me the error when you do the above. – intellignt_idiot Dec 26 '19 at 07:29
0

Try this code(It's 3.3.0 version please check in github link which I given below for new one) :

Full code

private BannerView.IListener bannerAdViewListener= createBannerListener();
@Override
public View onCreateView(
    @NonNull LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_unity_banners, container, false);
    this.bannerView= root.findViewById(R.id.top_banner_button);
   int widthInDp = Math.round(ViewUtilities.dpFromPx(self.getContext(), self.topBannerContainer.getWidth()));
                int heightInDp = Math.round(ViewUtilities.dpFromPx(self.getContext(), self.topBannerContainer.getHeight()));
                UnityBannerSize unityBannerSize = new UnityBannerSize(widthInDp, heightInDp);
    bannerView= new BannerView(getActivity(), "bannerads", unityBannerSize);
                bannerView.setListener(bannerAdViewListener);
                bannerView.load();
}

private BannerView.IListener createBannerListener() {
    final UnityBannersFragment self = this;
    return new BannerView.Listener() {
        @Override
        public void onBannerFailedToLoad(BannerView bannerAdView, BannerErrorInfo errorInfo) {
            if (self.topBannerView != null && self.topBannerView == bannerAdView) {
                self.topBannerContainer.removeView(self.topBannerView);
                self.topBannerView.destroy();
                self.topBannerView = null;
                self.topBannerButton.setText(R.string.show_top_banner);
            }
        }
        @Override
        public void onBannerLoaded(BannerView bannerAdView) {
            Log.d("UnityAdsExample", "onBannerLoded is called for: " + bannerAdView.getPlacementId());
        }

        @Override
        public void onBannerClick(BannerView bannerAdView) {
            Log.d("UnityAdsExample", "onBannerClick is called for: " + bannerAdView.getPlacementId());
        }

        @Override
        public void onBannerLeftApplication(BannerView bannerAdView) {
            Log.d("UnityAdsExample", "onBannerLeftApplication is called for: " + bannerAdView.getPlacementId());
        }
    };

}

For more details Use this sample from Unity github link

Bhavin Patel
  • 872
  • 13
  • 30