AdMob广告未在一项特定活动中展示

时间:2018-05-21 19:08:52

标签: java android xml admob

我正在尝试将AdMob广告实施到我的主要活动,但在模拟器和USB调试设备上运行应用后,没有可见的测试广告。我尝试将主要活动中的确切代码复制并粘贴到另一个活动上,广告可见且工作正常。我有2个水平线性布局,adView都在垂直线性布局中。我甚至可以通过将adView置于两个水平线性布局之间来更改顺序,并且在运行应用程序时,adView有空间但没有广告。 logcat甚至说广告已加载。

XML of Activity that doesn't work: 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp"
android:scrollbars="vertical"
tools:context="com.softwoodapps.app.MainActivity">

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />

活动的JAVA不起作用:

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


    AdView adView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("0E9B6533ACE6F6B6F4E3DF0BEAF0DEBD")
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

    adView.loadAd(adRequest);
}

完整XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp"
android:scrollbars="vertical"
tools:context="com.baseballscore.softwoodapps.baseballscore.MainActivity">

<LinearLayout
    android:id="@+id/topInningLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="false"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="false"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/homeNameView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="8dp"
            android:gravity="center_horizontal"
            android:text="@string/textHome"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/homeScoreView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:gravity="center_horizontal"
            android:text="@string/homeScoreText"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="56sp" />

        <Button
            android:id="@+id/homeRunButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="16dp"
            android:onClick="addRunHome"
            android:text="@string/homeRunButton"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/topInningView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="TOP"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/inningView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:fontFamily="sans-serif-light"
            android:gravity="center"
            android:text="@string/inningview"
            android:textColor="#000000"
            android:textIsSelectable="false"
            android:textSize="54sp" />

        <TextView
            android:id="@+id/bottomInningView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="BOTTOM"
            android:visibility="invisible" />

        <Button
            android:id="@+id/btnInning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addInning"
            android:text="@string/inningtext"
            android:textSize="22sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/awayNameView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="8dp"
            android:gravity="center_horizontal"
            android:text="@string/awaytext"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/awayScoreView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:gravity="center_horizontal"
            android:text="@string/awayscoretetxt"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="56sp" />

        <Button
            android:id="@+id/awayRunButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="8dp"
            android:onClick="addRunAway"
            android:text="@string/awayButton"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/outstrikelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="false"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="false"
    android:layout_below="@+id/topInningLayout"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:paddingTop="10dp">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">


        <Button
            android:id="@+id/btnOuts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="4dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="4dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:maxHeight="0dp"
            android:minHeight="0dp"
            android:onClick="addOut"
            android:text="@string/outstext"
            android:textSize="24sp" />

        <Button
            android:id="@+id/btnStrike"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="4dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="4dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:maxHeight="0dp"
            android:minHeight="0dp"
            android:onClick="addStrike"
            android:text="@string/striketext"
            android:textSize="24sp" />

        <Button
            android:id="@+id/btnBalls"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnStrike"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="4dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="4dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:maxHeight="0dp"
            android:minHeight="0dp"
            android:onClick="addBall"
            android:text="@string/ballbutton"
            android:textSize="24sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/outsView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:fontFamily="sans-serif-medium"
            android:gravity="center"
            android:text="@string/outsviewtext"
            android:textSize="50sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/strikeView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:fontFamily="sans-serif-medium"
            android:gravity="center"
            android:text="@string/strieview"
            android:textSize="50sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/ballView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:fontFamily="sans-serif-medium"
            android:gravity="center"
            android:text="@string/ballview"
            android:textSize="50sp"
            android:textStyle="bold" />

    </LinearLayout>



</LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在我声明了一堆其他变量之后,我发现广告代码(声明和loadRequest)必须在OnCreate()方法的末尾。