调用了Admob onAdLoaded,但屏幕上未显示任何内容

时间:2018-09-13 11:19:31

标签: android

我正尝试使用他们提供的示例测试ID来使用Admob。我已经在项目级别build.gradle中添加了maven存储库,并在app build.gradle中实现了播放服务广告。当我运行该应用程序时。调用了onAdLoaded方法。但我的屏幕上没有任何东西。我也尝试过使用自己的唯一广告ID。但结果是一样的。请帮助

  

主要活动

 package com.twotwotwo.dip.admobtry;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdSize;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.MobileAds;

    public class MainActivity extends AppCompatActivity {
        AdView mAdView;

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

            MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

            mAdView = new AdView(this);
            mAdView.setAdSize(AdSize.BANNER);
            mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

            AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
            mAdView.loadAd(adRequest);

            mAdView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    // Code to be executed when an ad finishes loading.
                    showToast("Ad loaded");
                    if (mAdView.getVisibility() == View.GONE) {
                        mAdView.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // Code to be executed when an ad request fails.
                    if(errorCode == AdRequest.ERROR_CODE_INTERNAL_ERROR){
                        showToast(" Something happened internally");
                    } else if(errorCode == AdRequest.ERROR_CODE_INVALID_REQUEST){
                        showToast("The ad request was invalid; for instance, the ad unit ID was incorrect.");
                    } else if(errorCode == AdRequest.ERROR_CODE_NETWORK_ERROR){
                        showToast("The ad request was unsuccessful due to network connectivity.");
                    } else if(errorCode == AdRequest.ERROR_CODE_NO_FILL){
                        showToast("The ad request was successful, but no ad was returned due to lack of ad inventory.");
                    }
                }

                @Override
                public void onAdOpened() {
                    // Code to be executed when an ad opens an overlay that
                    // covers the screen.
                }

                @Override
                public void onAdLeftApplication() {
                    // Code to be executed when the user has left the app.
                }

                @Override
                public void onAdClosed() {
                    // Code to be executed when when the user is about to return
                    // to the app after tapping on an ad.
                }
            });
        }

        private void showToast(String message){
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
        }
    }

1 个答案:

答案 0 :(得分:1)

您缺少此行

mAdView=(AdView) findViewbyid(R.id.adView)