0

我使用下面的代码在 Android 中添加 Admob。但不能工作只得到黑屏。

主布局.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/linearlayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

MainActivity.java

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout1);
        adView = new AdView(this, AdSize.SMART_BANNER, "xxxxxxxxxxxxx");
        layout.addView(adView);
        setContentView(layout);
        new Thread(){
            @SuppressWarnings("deprecation")
            public void run() {
                Looper.prepare();
                AdRequest re = new AdRequest();
                re.setTesting(true);
                adView.loadAd(re);
            };
        }.start();  
    }

清单.xml

 <activity
        android:name="com.google.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
    </activity>

日志猫:

07-16 12:32:09.839: I/Ads(1298): To get test ads on this device, call adRequest.addTestDevice("DB5C6285034D8192153E5D66D726E418");
07-16 12:32:10.629: I/Ads(1298): adRequestUrlHtml: <html><head><script src="http://www.gstatic.com/afma/sdk-core-v40.js"></script><script>AFMA_buildAdURL({"preqs":0,"u_sd":1,"slotname":"S5830c763db75","u_w":320,"msid":"syncfusion.test.testAdmobDemo","js":"afma-sdk-a-v4.1.0","isu":"DB5C6285034D8192153E5D66D726E418","format":"320x50_mb","net":"wi","app_name":"1.android.syncfusion.test.testAdmobDemo","hl":"en","u_h":480,"u_audio":3,"u_so":"p"});</script></head><body></body></html>
4

1 回答 1

0

第一次 - 你调用 setContentView 两次。删除第二个电话。

第二 - 您需要添加一个广告侦听器:

AdListener adListener = new AdListener()
{
    @Override
    public void onReceiveAd(Ad arg0)
    {
        // do something
    }

    @Override
    public void onPresentScreen(Ad arg0)
    {
        // do something
    }

    @Override
    public void onLeaveApplication(Ad arg0)
    {
        // do something
    }

    @Override
    public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1)
    {
        // do something             
    }

    @Override
    public void onDismissScreen(Ad arg0)
    {
        // do something
    }
};

add.setAdListener(adListener);

现在它应该工作了。

或者,您可以尝试在活动的 xml 中创建广告:

<com.google.ads.AdView xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/backup_ad"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    googleads:adSize="BANNER"
    googleads:adUnitId="@string/admob_id"
    android:gravity="center_horizontal" />
于 2013-07-16T09:50:02.500 回答