我尝试在我的应用中添加admob广告。我首先添加代码:
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("abc")
.build();
adView.loadAd(adRequest);
11-23 11:16:20.285: E/AndroidRuntime(24361): FATAL EXCEPTION: main
11-23 11:16:20.285: E/AndroidRuntime(24361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.phohaycom/com.example.phohay.activity.ReadingActivity2}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
所以我尝试添加代码:
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("ca-app-pub-1845170371498416/12345678");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("D5AF9D9AFD269EF3B817CD9804BF2CE9")
.build();
mAdView.loadAd(adRequest);
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
它在任何地方都失败了?请帮帮我
答案 0 :(得分:0)
我认为移除ads:adSize="BANNER"
或mAdView.setAdSize(AdSize.BANNER);
会有所帮助。
答案 1 :(得分:0)
我做到了。 我不在xml中添加代码。相反,我创建线性布局并在代码中添加广告
AdView adView = new AdView(getApplication());
adView.setAdUnitId("ca-app-pub-1845170371498416/123456789");
adView.setAdSize(AdSize.BANNER);
LinearLayout adLinLay = (LinearLayout) findViewById(R.id.Llayout);
adLinLay.addView(adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
"D5AF9D9AFD269EF3B817CD9804BF2CE9").build();
adView.loadAd(adRequest);
答案 2 :(得分:0)
删除这个:
for i in range(10):
print str(i) + ",",
删除 xmlns:ads ***标记
添加这一个:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xml文件应如下所示:
xmlns:ads="http://schemas.android.com/apk/res-auto"
希望这会对你有所帮助。