简体   繁体   English

Admob广告永远不会在视图中显示

[英]Admob ad never displays in View

I am trying to get AdMob ads to display in my android application and have been unable to do so. 我正在尝试将AdMob广告显示在我的Android应用程序中,并且无法这样做。 I have contacted their support and have not received any response in almost a week, so I figured I would now ask for help here. 我已经联系了他们的支持,并且在将近一周内没有收到任何回复,所以我想我现在会在这里寻求帮助。

First some code: 首先是一些代码:
AndroidManifest.xml

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:versionCode="1" package="com.foo.application">
  <application>
    <meta-data 
        android:value="admob-publisher-id-here" 
        android:name="ADMOB_PUBLISHER_ID" />
  </application>

  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Yes, admob-publisher-id-here is my real publisher id in the actual manifest file. 是的, admob-publisher-id-here是我在实际清单文件中的真正发布者ID。

main_layout.xml

<LinearLayout 
    android:id="@+id/adhost" 
    android:layout_width="fill_parent"
    android:padding="5dip" android:layout_height="wrap_content"
    android:minHeight="20dip" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res/com.foo.application">
  <com.admob.android.ads.AdView 
      android:id="@+id/ad" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      app:backgroundColor="#000000" 
      app:primaryTextColor="#FFFFFF" 
      app:secondaryTextColor="#CCCCCC"
      app:keywords="android at&amp;t t-mobile iphone blah"/>
</LinearLayout>

attr.xml

<resources>
  <declare-styleable name="com.admob.android.ads.AdView">            
    <attr name="backgroundColor" format="color" />
    <attr name="primaryTextColor" format="color" />
    <attr name="secondaryTextColor" format="color" />
    <attr name="keywords" format="string" />
    <attr name="refreshInterval" format="integer" />
  </declare-styleable>
</resources>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;

import com.foo.application.R;

public class MainActivity extends Activity {
  private AdView ad;

  public MainActivity ( ) {
        AdManager.setTestDevices ( new String[] {
      // made this up for this
            "012345678994814751742145548AAAAAAA" 
        } );
        AdManager.setTestAction ( "url" );
    }

    @Override
    public void onCreate ( Bundle savedInstanceState ) {
      super.onCreate ( savedInstanceState );
        setContentView(R.layout.main_layout);

        if ( AdManager.isTestDevice ( this ) ) {
          // this logs
            Log.w ( "foo-app", "we are on a test device" );
        }

        ad = ( AdView ) this.findViewById ( R.id.ad );
        if ( ad != null ) {
            ad.setVisibility ( View.VISIBLE );
            ad.setAdListener ( new AdListener () );
        }
    }
}

AdListener.java

package com.foo.application

import android.util.Log;

import com.admob.android.ads.AdView;
import com.admob.android.ads.SimpleAdListener;

class AdListener extends SimpleAdListener {
    @Override
    public void onFailedToReceiveAd ( AdView adView ) {
      // this is what logs
        Log.w ( "foo-app", "failed to receive ad" );
        super.onFailedToReceiveAd ( adView );
    }

    @Override
    public void onFailedToReceiveRefreshedAd ( AdView adView ) {
        Log.w ( "foo-app", "failed to receive refreshed ad" );
        super.onFailedToReceiveRefreshedAd ( adView );
    }

    @Override
    public void onReceiveAd ( AdView adView ) {
        Log.w ( "foo-app", "receive ad" );
        super.onReceiveAd ( adView );
    }

    @Override
    public void onReceiveRefreshedAd ( AdView adView ) {
        Log.w ( "foo-app", "receive refreshed ad" );
        super.onReceiveRefreshedAd ( adView );
    }
}

In the logcat, i see the following: 在logcat中,我看到以下内容:

INFO/AdMobSDK(29541): To get test ads on this device use AdManager.setTestDevices( new String[] { "012345678994814751742145548AAAAAAA" } )
WARN/AdMobSDK(29541): Ignoring requestFreshAd() because we are requesting an ad right now already.
WARN/AdMobSDK(29541): Ignoring requestFreshAd() because we are requesting an ad right now already.
WARN/foo-app(29541): we are on a test device
WARN/foo-app(29541): failed to receive ad

The AdListener is indicating that it is unable to retrieve the ad. AdListener表示无法检索广告。 I have an active data connection, plus I am in Test Mode, so it is supposed to always show an ad, according to the admob wiki . 根据admob wiki ,我有一个活跃的数据连接,而且我处于测试模式,所以应该总是显示一个广告。 There are no errors in the logcat, nor is the application force closing at all. logcat中没有错误,应用程序强制也根本没有关闭。

Anyone have any ideas or do you see something I am doing wrong? 任何人都有任何想法,或者你看到我做错了什么?

update: I got a response from admob, but all they said was i was creating the aeveiw twice, which I am not 更新:我收到了来自admob的回复,但他们所说的只是我创造了两次,我不是

Admob should have included this as a warning, but I digress... Admob应该将此作为警告,但我离题了......

In your layout you have android:padding="5dip" this of course causes phones like the G1 to 'lose' some of their available screen real-estate (below 320dip width). 在你的布局中你有android:padding="5dip"这当然会导致像G1这样的手机“丢失”一些可用的屏幕空间(低于320dip宽度)。 Which is of course the minimum width for an admob ad, causing it to fail. 这当然是admob广告的最小宽度,导致它失败。 Then calling onFailedToReceiveAd in the listener with no explanation whatsoever. 然后在监听器中调用onFailedToReceiveAd ,无需任何解释。

The logs only contain 日志只包含

WARN/AdMobSDK(347): Ignoring requestFreshAd() because we are requesting an ad right now already.

The fix is simple, don't use padding or margins in the root of your layouts that contain ads. 修复很简单,不要在包含广告的布局的根目录中使用填充或边距。 Took some experimentation, but it works. 进行了一些实验,但它确实有效。

Ryan have you set your Admob publisher Id??? Ryan你有没有设置你的Admob出版商Id ???

 <meta-data android:value="YOUR_ADMOB_PUBLISHER_ID" 
        android:name="ADMOB_PUBLISHER_ID" />

Try getting rid of 试着摆脱

    if ( ad != null ) {
        ad.setVisibility ( View.VISIBLE );
        ad.setAdListener ( new AdListener () );
    }

and seeing if it works, as mine is identical without those lines and works fine. 并且看它是否有效,因为我的相同没有那些线并且工作正常。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent" android:id="@+id/rltvLayout1"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayoutwebview"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:orientation="vertical">
        <WebView android:id="@+id/webView"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:fitsSystemWindows="true" />
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/ad_layout" android:layout_height="wrap_content"
        android:gravity="bottom" android:layout_alignParentBottom="true"
        android:layout_alignBottom="@+id/home_layout">
        <com.google.ads.AdView android:layout_width="wrap_content"
            android:layout_height="wrap_content" ads:adUnitId="put here your id"
            ads:adSize="BANNER" android:id="@+id/adView" ads:refreshInterval="60" />

    </LinearLayout>
</RelativeLayout>

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

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