简体   繁体   中英

AdMob Ads not showing in one specific activity

I am trying to implement an AdMob ad to my main activity but after running the app on the emulator and a USB debugging device, no test ad is visible. I tried copying and pasting the exact code from the main activity on to another activity and the ad was visible and worked fine. I have 2 horizontal linear layouts and the adView all inside a vertical linear layout. I can even change the order by putting the adView in between the 2 horizontal linear layouts and there is room for the adView but no ad when the app is run. The logcat even says the ad is loaded.

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 of Activity that does not work:

 @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);
}

Full 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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM