我为我的Android应用程序使用AdMob。我想将AdView添加到扩展ListActivity的Activity。将AdView添加到扩展ListActivity的类中
我已经尝试使用getListView().addHeaderView(my_ad_view)
但似乎没有工作。
任何想法如何实现这一目标?由于我对这个领域非常陌生,任何帮助都将不胜感激。谢谢。
这里是我的代码:
此活动开始,如:
public class Home extends ListActivity { /* Code */ }
的onCreate()
功能:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the list items. The method populateList() reads the items from the database
// and returns a String array.
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, populateList()));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
goToTasks(String.valueOf(getListAdapter().getItem(position)));
}
});
lv.setCacheColorHint(0);
lv.setBackgroundResource(R.drawable.fmn_background_300);
registerForContextMenu(getListView());
refreshList();
// Here is the AdView code
AdView adView = new AdView(this, AdSize.BANNER, FMN.ADMOB_ID);
LinearLayout ll = (LinearLayout)findViewById(R.id.ll_admob);
ll.addView(adView);
getListView().addHeaderView(ll);
adView.loadAd(new AdRequest());
}
我已经设置了权限,并已宣布在表现为活动需要。
在populateList()
方法中,我只获得Cursor
,然后做一些操作工作,最后返回一个String
数组。
那么我做错了什么?该活动加载时,应用程序才会退出。