Java Admob 广告不会加载。错误 2. 以前可以用,但突然停止了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27729547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:55:58  来源:igfitidea点击:

Admob ad will not load. Error 2. It worked before but suddenly stopped

javaandroidadmob

提问by Chuck Onwuzuruike

Here is the entire class that I am trying to load a banner ad into (createAd() method does the work)

这是我试图将横幅广告加载到的整个类(createAd() 方法完成工作)

public class HomeActivity extends Activity {

    private HomeView homeView;
    private ImageView playButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

//        playButton = (ImageView)findViewById(R.id.playButton);
//        playButton.setImageBitmap(BitmapFactory.decodeResource(getResources(),
//                R.drawable.play));
//        setListener();
        setContentView(R.layout.home_main);
        createAd();

    }

    private void setListener (){
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playButton.setImageBitmap(BitmapFactory.decodeResource(getResources(),
                        R.drawable.play2));
                playGame();
            }
        });
    }

    private void createAd(){
        setContentView(R.layout.home_main);
        AdView adView = (AdView)findViewById(R.id.bottomAd);
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        adRequestBuilder.addTestDevice("FEACCF89D31180ABDD853C9C3DD2E871");
//                .addTestDevice("1744314CD0C7B0619F94D44CE2E5093E");

        adView.loadAd(adRequestBuilder.build());
    }

    public void playGame() {
        SharedPreferences savedData = getApplicationContext()
                .getSharedPreferences("lastScore", Context.MODE_PRIVATE);
        int score = savedData.getInt("lastScore", Context.MODE_PRIVATE);
        Intent intent = new Intent(HomeActivity.this, GamePlay.class);
        intent.putExtra("lastScore", score);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Here is the xml of home_main

这是home_main的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/homeRelative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeActivity">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottomAd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_banner_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

Here is my log after the most recent run

这是我最近一次运行后的日志

 01-01 16:00:32.235  26387-26387/com.rune.colorhunt D/OpenGLRenderer﹕ Enabling debug mode 0
01-01 16:00:32.275      652-796/? I/CrashAnrDetector﹕ onPackageUpdateFinished : com.rune.colorhunt
01-01 16:00:37.351  26387-26416/com.rune.colorhunt D/dalvikvm﹕ DexOpt: --- BEGIN 'ads976472321.jar' (bootstrap=0) ---
01-01 16:00:37.361  26387-26387/com.rune.colorhunt V/WebViewChromium﹕ Binding Chromium to the main looper Looper (main, tid 1) {41d00b38}
01-01 16:00:37.361  26387-26387/com.rune.colorhunt I/chromium﹕ [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
01-01 16:00:37.361  26387-26387/com.rune.colorhunt I/BrowserProcessMain﹕ Initializing chromium process, renderers=0
01-01 16:00:37.391  26387-26426/com.rune.colorhunt W/chromium﹕ [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
01-01 16:00:37.421  26387-26387/com.rune.colorhunt I/Ads﹕ Starting ad request.
01-01 16:00:37.441  26387-26416/com.rune.colorhunt D/dalvikvm﹕ DexOpt: --- END 'ads976472321.jar' (success) ---
01-01 16:00:37.441  26387-26416/com.rune.colorhunt D/dalvikvm﹕ DEX prep '/data/data/com.rune.colorhunt/cache/ads976472321.jar': unzip in 0ms, rewrite 91ms
01-01 16:00:37.611  26387-26394/com.rune.colorhunt I/dalvikvm﹕ Total arena pages for JIT: 11
01-01 16:00:37.611  26387-26394/com.rune.colorhunt I/dalvikvm﹕ Total arena pages for JIT: 12
01-01 16:00:37.621  26387-26394/com.rune.colorhunt I/dalvikvm﹕ Total arena pages for JIT: 13
01-01 16:00:37.621  26387-26394/com.rune.colorhunt I/dalvikvm﹕ Total arena pages for JIT: 14
01-01 16:00:37.651  26387-26431/com.rune.colorhunt W/Ads﹕ There was a problem getting an ad response. ErrorCode: 2
01-01 16:00:37.691  26387-26387/com.rune.colorhunt I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.
01-01 16:00:37.691  26387-26387/com.rune.colorhunt W/Ads﹕ Failed to load ad: 2
01-01 16:01:37.715  26387-26387/com.rune.colorhunt I/Ads﹕ Starting ad request.
01-01 16:01:37.825  26387-26490/com.rune.colorhunt W/Ads﹕ There was a problem getting an ad response. ErrorCode: 2
01-01 16:01:37.845  26387-26387/com.rune.colorhunt I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.
01-01 16:01:37.845  26387-26387/com.rune.colorhunt W/Ads﹕ Failed to load ad: 2
01-01 16:02:37.889  26387-26387/com.rune.colorhunt I/Ads﹕ Starting ad request.
01-01 16:02:38.009  26387-26531/com.rune.colorhunt W/Ads﹕ There was a problem getting an ad response. ErrorCode: 2
01-01 16:02:38.039  26387-26387/com.rune.colorhunt I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.
01-01 16:02:38.039  26387-26387/com.rune.colorhunt W/Ads﹕ Failed to load ad: 2
01-01 16:03:38.972  26387-26387/com.rune.colorhunt I/Ads﹕ Starting ad request.
01-01 16:03:39.062  26387-26783/com.rune.colorhunt W/Ads﹕ There was a problem getting an ad response. ErrorCode: 2
01-01 16:03:39.062  26387-26387/com.rune.colorhunt I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.
01-01 16:03:39.062  26387-26387/com.rune.colorhunt W/Ads﹕ Failed to load ad: 2

If somebody would help me, that would be amazing. I literally have no idea what the problem could be. Perhaps im missing something very simple and just need another pair of eyes. Any help is appreciated, thanks.

如果有人愿意帮助我,那就太棒了。我真的不知道问题是什么。也许我错过了一些非常简单的东西,只需要另一双眼睛。任何帮助表示赞赏,谢谢。

回答by Amrut Bidri

at first be clear about which type of add you want to load, interstitial or banner ad. if you want to load banner ad, create a banner ad_unit_id on admob site, then use AdView directly inside your activity view as below:

首先要明确您要加载哪种类型的广告,插页式广告或横幅广告。如果您想加载横幅广告,请在 admob 网站上创建横幅 ad_unit_id,然后直接在您的活动视图中使用 AdView,如下所示:

 <com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/xadView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/admob_banner_ad_unit_id" />

and just request to load banner ad from your code as below: write this code in your activity inside oncreate after setcontentview

只需请求从您的代码中加载横幅广告,如下所示:在 setcontentview 之后在 oncreate 内的活动中编写此代码

AdView mAdView = (AdView) findViewById(R.id.xadView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
                AdRequest.DEVICE_ID_EMULATOR).build();
mAdView.loadAd(adRequest); 

AND

if you want to load interstial ads, create a interstitial ad_unit_id on admob site, then use it in below method:

如果要加载插页式广告,请在 admob 站点上创建一个插页式 ad_unit_id,然后在以下方法中使用它:

public void showFullScreenAd() {
    try {

                com.google.android.gms.ads.InterstitialAd interstitial = new com.google.android.gms.ads.InterstitialAd(context);
                interstitial
                        .setAdUnitId(ADMOB_INTERSTITIAL_AD_UNIT_ID);

                // Check the logcat output for your hashed device ID to get test ads
                // on
                // a physical device.
                com.google.android.gms.ads.AdRequest adRequest = new AdRequest.Builder()
                        .build();

                // Load the interstitial ad.
                interstitial.loadAd(adRequest);
                interstitial
                        .setAdListener(new com.google.android.gms.ads.AdListener() {
                            @Override
                            public void onAdLoaded() {
                                interstitial.show();
                            }                           
                        });

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

NOTE: important thing is that sometimes we use banner ad unit id inside interstitial ads or interstitial ad unit id inside banner ads, so this error occurs.

注意:重要的是,有时我们在插页式广告中使用横幅广告单元 ID 或在横幅广告中使用插页式广告单元 ID,因此会发生此错误。

回答by NickUnuchek

Admob ad will not load. Error 2 appears if date or time is wrong on your device, just set autodate correcting on your device

Admob 广告不会加载。如果您的设备上的日期或时间错误,则会出现错误 2,只需在您的设备上设置自动日期校正

回答by MikeL

Check that your device has a proper internet connection. Without internet connection you will not get ads.

检查您的设备是否具有正确的互联网连接。没有互联网连接,您将不会收到广告。

回答by Ionut Negru

Another issue could be an adblocker application as suggested in this post: https://stackoverflow.com/a/36010639/3596576by @Durian.

另一个问题可能是此帖子中建议的广告拦截器应用程序: https://stackoverflow.com/a/36010639/3596576 by @Durian。

For me this was the issue.

对我来说,这就是问题所在。

回答by Erdem

I had the similar problem. The internet connection was OK and there was no ad blocker installed.

我有类似的问题。互联网连接正常,没有安装广告拦截器。

The problem might be related with your test device. Check if you have another application that contains Admob with your test device. You can also check with another device and another app that contains Admob.

问题可能与您的测试设备有关。检查您的测试设备是否有另一个包含 Admob 的应用程序。您还可以使用另一台设备和另一个包含 Admob 的应用程序进行检查。

In my case I realized that my test device has the internet connection but doesn't show up any Admob ads in different applications. So I tried to restart my test device and It worked.

就我而言,我意识到我的测试设备有互联网连接,但没有在不同的应用程序中显示任何 Admob 广告。所以我尝试重新启动我的测试设备并且它起作用了。

回答by kalandar

Error no 2 means ERROR_CODE_NETWORK_ERROR. so check your internet connection and still not showing ads then just restart your mobile and check...that was worked for me.

错误号 2 表示ERROR_CODE_NETWORK_ERROR. 因此,请检查您的互联网连接,但仍未显示广告,然后只需重新启动您的手机并检查...这对我有用。

回答by S?n Nguy?n tr??ng

This behavior is being caused by an app making too many ad requests at once, which puts the SDK into a bad state. Restarting Google play services, which can effectively be done by clearing all data, fixes the issue for me. The SDK fix for this issue will be included in the next Google play services release. Once your device picks up the update, you won't need to update your app. this is known issue of sdk

此行为是由应用程序一次发出过多广告请求引起的,这使 SDK 处于不良状态。重新启动谷歌播放服务,这可以通过清除所有数据来有效地完成,为我解决了这个问题。此问题的 SDK 修复将包含在下一个 Google Play 服务版本中。一旦您的设备接收到更新,您就无需更新您的应用程序。 这是 sdk 的已知问题

回答by Velda

If using real device, check also other apps, you know they contain ads, if ads are shown. Also check individually on mobile network and on Wi-Fi.

如果使用真实设备,还要检查其他应用程序,如果显示广告,您知道它们包含广告。还要单独检查移动网络和 Wi-Fi。

For me, it does not show only on mobile network and in all apps. Deleting data of Google Play Serviceshelp me, and ads starts showing again. Until GPS downloads its data back. So it worked for about one minute. :-D

对我来说,它不仅仅显示在移动网络和所有应用程序中。删除 Google Play 服务的数据对我有帮助,广告又开始显示。直到 GPS 下载回它的数据。所以它工作了大约一分钟。:-D

回答by anand

I had the same issue of the ad not appearing on the physical test device screen.

我遇到了同样的问题,广告没有出现在物理测试设备屏幕上。

I switched off my wifi and started using 3g mobile data and then my ads started appearing on the physical test devices.

我关闭了 wifi 并开始使用 3g 移动数据,然后我的广告开始出现在物理测试设备上。

I also have a difference in dependecies:

我在依赖方面也有不同:

com.google.android.gms:play-services-ads:9.2.0

instead of 11.6.2

代替 11.6.2

回答by ramveer singh Narwariya

try 'disable background data on mobile networks' set as disable in google services, it work for me

尝试在谷歌服务中将“禁用移动网络上的后台数据”设置为禁用,它对我有用