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.