Android应用广告无法加载求助:测试设备请求后提示广告未加载
Hey there! Let's dig into why your interstitial ad isn't loading even though your logs confirm the request is sent from a test device. Here's a step-by-step breakdown of the most likely issues and fixes:
1. You're Missing the Critical Ad Load Call
Looking at your truncated code snippet, you've initialized the PublisherInterstitialAd and set the AdUnit ID—but you haven't triggered the actual ad load request. Ads won't load automatically; you need to explicitly call loadAd() to start the process.
2. Add an AdListener to Get Specific Failure Details
The generic "Ad did not load" toast doesn't tell you what went wrong. Adding an AdListener will give you a precise error code to diagnose the issue:
ERROR_CODE_NO_FILL: No ad inventory is available (rare with test IDs, but common with unapproved live IDs)ERROR_CODE_NETWORK_ERROR: Device has no stable internet connectionERROR_CODE_INVALID_REQUEST: Invalid AdUnit ID or malformed requestERROR_CODE_INTERNAL_ERROR: AdMob SDK encountered an unexpected issue
3. Verify Your AdUnit ID
- For testing, use AdMob's official test interstitial ID:
ca-app-pub-3940256099942544/1033173712(this guarantees test ads will load if everything else is set up correctly) - Double-check that the value in
strings.xmlforinterstitial_ad_unit_idis spelled correctly and matches your AdMob dashboard (if using a live ID)
4. Confirm Required Permissions
Make sure these permissions are added to your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5. Check Your SDK Dependency
Ensure you're using a recent version of the Google Mobile Ads SDK in your module-level build.gradle:
dependencies { implementation 'com.google.android.gms:play-services-ads:22.6.0' // Use the latest version available }
Full Working Example Code
Here's how to update your Ads class with all the necessary pieces:
public class Ads extends AppCompatActivity { PublisherInterstitialAd mPublisherInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ads); // Replace with your actual layout mPublisherInterstitialAd = new PublisherInterstitialAd(this); mPublisherInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); // Set up listener to track ad status mPublisherInterstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { Toast.makeText(Ads.this, "Ad loaded successfully!", Toast.LENGTH_SHORT).show(); } @Override public void onAdFailedToLoad(int errorCode) { String errorMessage = switch(errorCode) { case AdRequest.ERROR_CODE_INTERNAL_ERROR -> "Internal SDK error"; case AdRequest.ERROR_CODE_INVALID_REQUEST -> "Invalid AdUnit ID or request"; case AdRequest.ERROR_CODE_NETWORK_ERROR -> "Network connection issue"; case AdRequest.ERROR_CODE_NO_FILL -> "No ads available right now"; default -> "Unknown error"; }; Toast.makeText(Ads.this, "Ad failed to load: " + errorMessage, Toast.LENGTH_LONG).show(); } @Override public void onAdClosed() { // Reload ad after closing for next use loadInterstitialAd(); } }); // Trigger the ad load loadInterstitialAd(); } private void loadInterstitialAd() { AdRequest adRequest = new AdRequest.Builder().build(); mPublisherInterstitialAd.loadAd(adRequest); } // Example method to show ad when a button is clicked public void showAd(View view) { if (mPublisherInterstitialAd.isLoaded()) { mPublisherInterstitialAd.show(); } else { Toast.makeText(this, "Ad not ready yet—reloading!", Toast.LENGTH_SHORT).show(); loadInterstitialAd(); } } }
内容的提问来源于stack exchange,提问作者Anna Kalinina

