简体   繁体   English

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

[英]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. 我正在尝试在我的主要活动中实施AdMob广告,但是在模拟器和USB调试设备上运行应用后,看不到任何测试广告。 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. 我有2个水平线性布局,而adView全部位于垂直线性布局内。 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. 我什至可以通过将adView放在2个水平线性布局之间来更改顺序,并且adView有空间,但在运行应用程序时没有广告。 The logcat even says the ad is loaded. 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 of Activity that does not work: 活动的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);
}

Full XML 完整的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()方法的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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