我使用以下代码在Android中添加Admob。但是没有工作只能获得blanck屏幕。
mainlayout.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();
}
的Manifest.xml
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
logcat的:
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>
答案 0 :(得分:0)
1st - 您正在调用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" />