My Android app is crashing after implementing Interstitial ads

136 views
Skip to first unread message

Joaquim Mussassa

unread,
Jun 15, 2023, 6:56:20 AM6/15/23
to Google Mobile Ads SDK Developers
I followed the tutorial for ADmob Interstitial ads (on their site) and my app started crashing on the emulator. Here is the code:
package com.urbannet.kubulabotfree;

import android.app.DownloadManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

import kubulabotfree.R;

public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
private InterstitialAd mInterstitialAd;
private WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i(TAG,"onAdLoaded");
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.d(TAG, loadAdError.toString());
mInterstitialAd = null;
}
});

mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdClicked() {
//Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}

@Override
public void onAdDismissedFullScreenContent() {
//Called when ad is dismissed.
//Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
mInterstitialAd = null;
}

@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
//Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
mInterstitialAd = null;
}

@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG,"Ad showed fullscreen content.");
}
});

if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}

myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();

webSettings.setJavaScriptEnabled(true);
//improve webView performance
//myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
//myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//myWebView.getSettings().setAppCacheEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
//webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
//webSettings.setUseWideViewPort(true);
//webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
//webSettings.setEnableSmoothTransition(true);

myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.loadUrl("https://kubulabotfree.soundtrip.store/");
//myWebView.setWebViewClient(new WebViewClient());

myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

if((String.valueOf(request.getUrl())).contains("https://kubulabotfree.soundtrip.store/")) {
view.loadUrl(String.valueOf(request.getUrl()));
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
}

return true;
}
});

}

@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

final WebView.HitTestResult webViewHitTestResult = myWebView.getHitTestResult();

if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

contextMenu.setHeaderTitle("Download Image From Below");

contextMenu.add(0, 1, 0, "Save - Download Image")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {

String DownloadImageURL = webViewHitTestResult.getExtra();

if (URLUtil.isValidUrl(DownloadImageURL)) {

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

Toast.makeText(MainActivity.this, "Image Downloaded Successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Sorry.. Something Went Wrong.", Toast.LENGTH_LONG).show();
}
return false;
}
});
}

}

@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();

}

}

}

Mobile Ads SDK Forum Advisor

unread,
Jun 19, 2023, 2:40:42 PM6/19/23
to jcmus...@gmail.com, google-adm...@googlegroups.com

Hi,

Thank you for reaching out to us.

For us to better check this in our end, we may request the following information below aside from the code you've used below.

  • Sample app project that the crashes is reproducible
  • Steps to replicate
  • SDK versions
  • App ID
  • Device name and versions affected
  • Full stack trace of the crashes

You can provide the following details via reply on this thread or directly provide it to the link below.

If the file(s) you are looking to share are less than 25mb in total you can attach them to this case on your next reply. If you are having trouble attaching your file to this case or if your file(s) are larger than 25mb, you can share your files with me by performing the following steps:

1. Navigate to https://docs.google.com/forms/d/e/1FAIpQLSfkAiXMeYP-fw1W3Z-tT9uwmATEKO5X6S-th0gR2ezdKaaqfg/viewform?usp=pp_url&entry.400550049=Mobile+Ads+SDK&entry.460850823=5004Q00002mDI8FQAW&entry.80707362=00184419

2. Fill out all fields, and attach your file(s).

3. Please reply back on this thread when you have uploaded your file(s). Please do not share this link. 

This message is in relation to case "ref:_00D1U1174p._5004Q2mDI8F:ref"

Thanks,
 
Google Logo Mobile Ads SDK Team


Reply all
Reply to author
Forward
0 new messages