Admob Banner未出现在我的mainActivity中

时间:2017-08-09 07:31:49

标签: android admob

我按照官方文档:

https://developers.google.com/admob/android/banner

但我在代码中找不到错误。我试图改变

AdRequest adRequest = new AdRequest.Builder().build();

对于测试AdRequest,但它仍无效。

这是我的源代码:

https://github.com/sssolammm/HelloWordTestBanner

public class MainActivity extends AppCompatActivity {

private AdView mAdView;

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

    MobileAds.initialize(this, "ca-app-pub-5676983055182772~7282283135");

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

    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
            Log.i("Ads", "onAdLoaded");
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Code to be executed when an ad request fails.
            Log.i("Ads", "onAdFailedToLoad");
        }

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

        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
            Log.i("Ads", "onAdLeftApplication");
        }

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, 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);
}

/** Called when leaving the activity */
@Override
public void onPause() {
    if (mAdView != null) {
        mAdView.pause();
    }
    super.onPause();
}

/** Called when returning to the activity */
@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
        mAdView.resume();
    }
}

/** Called before the activity is destroyed */
@Override
public void onDestroy() {
    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}
}

Xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            tools:context=".MainActivity"
            tools:ignore="MergeRootFrame">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:text="@string/hello_world" />

<com.google.android.gms.ads.AdView
    android:id="@+id/ad_View"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您应该将测试设备ID添加到构建器。 如果它适用于测试ID,它将用于生产。

AdRequest request = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators .addTestDevice("your id") // Put here your test device id .build();

对于测试设备ID,运行app后进入Logcat,将Verbose放入搜索字段AdRequest中,这样id设备就会显示出来。

答案 1 :(得分:0)

好吧,有时广告没有显示...然后我卸载我的应用程序并将构建版本从调试更改为发布。

之后我再次安装我的应用程序...横幅广告将成功显示。

即使横幅广告没有显示..然后我检查日志信息...并获取测试设备ID ..将此测试设备ID添加到AdRequest对象...我确定将显示横幅广告。

adRequest.addTestDevice(“你的身份”)