代码之家  ›  专栏  ›  技术社区  ›  Ryan Conrad

Admob广告从不显示在视图中

  •  12
  • Ryan Conrad  · 技术社区  · 14 年前

    我试图让AdMob广告显示在我的android应用程序中,但一直无法做到这一点。我已经联系了他们的支持人员,已经快一个星期没有收到任何回复了,所以我想我现在应该在这里寻求帮助了。


    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>
    

    对, 此处为admob publisher 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 );
        }
    }
    

    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
    

    admob wiki . logcat中没有错误,应用程序也没有关闭。

    有人有什么想法吗?或者你看到我做错什么了吗?

    更新:我收到了admob的回复,但他们只说我创建了两次Aeview,我没有

    4 回复  |  直到 14 年前
        1
  •  20
  •   smith324    13 年前

    Admob本应将此作为警告,但我离题了。。。

    在你的布局中 android:padding="5dip" 这当然会导致像G1这样的手机“失去”一些可用的屏幕空间(低于320dip宽度)。这当然是admob广告的最小宽度,导致它失败。然后打电话来 onFailedToReceiveAd

    日志只包含

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

    解决方法很简单,不要在包含广告的版面根目录中使用填充或边距。做了些实验,但效果不错。

        2
  •  0
  •   stealthcopter    14 年前

    试着摆脱

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

        3
  •  0
  •   Android    13 年前
    <?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>
    
        4
  •  0
  •   Jorgesys    12 年前

    Ryan您设置了Admob发布者Id吗???

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