0

我正在尝试在我的 listactivity 中将 admob 设置为我的 listactivity 的标题我正在使用它来设置列表:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview, values);

和 (R.layout.listview) 只有这个:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
style="@style/Font"
android:gravity="right" >
</TextView>

它工作正常,然后我将其添加到设置 admob :

adView = new AdView(this, AdSize.BANNER, "xxxxxxx");
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());

但它给了我一个错误

我真的很困惑!

我尝试将 adView 设置为标题:

lv.addHeaderView(adView, null, false);

但这无济于事

请....有什么帮助吗?或分步教程。

4

1 回答 1

1

You're experiencing the crash because LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); is trying to find a LinearLayout element in your xml file with an id of mainLayout and it doesn't exist.

Embedding ads within a ListView is a bit complicated. You'll need to write your own ListAdapter to support ads. You can pull heavily from this AdMobListAdapter example which takes in your array adapter and embed ads every 10 list items. Using the example link above, your calling code would look like this (posted below for reference):

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview, values);
AdMobListAdapter adMobAdapter = new AdMobListAdapter(this, arrayAdapter);
setListAdapter(adMobAdapter);
于 2012-06-11T21:38:08.990 回答