Admob
SherlockFragmentActivity にバナー広告を読み込もうとしました。ロードしていません。しかし、appflood バナー広告を試してみると、うまくいきました。
最新のAdmob SDK 6.4.1
で試した後、古い SDK 6.2.1 で試しましたが、結果が得られませんでした。ログ cat にエラーは記録されませんでした。
以下は私のコードです:
public class SampleClass extends SherlockFragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
private AdView adView = null;
private boolean adShown = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setTab();
_mViewPager.setCurrentItem(1);
this.adShown = false;
AdUpdate();
}
private void setUpView(){
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(1);
}
private void setTab() {
_mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int position)
{}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{}
@Override
public void onPageSelected(int position)
{
switch(position) {
case 0:
findViewById(R.id.firstTab).setVisibility(View.VISIBLE);
findViewById(R.id.secondTab).setVisibility(View.INVISIBLE);
break;
case 1:
findViewById(R.id.firstTab).setVisibility(View.VISIBLE);
findViewById(R.id.secondTab).setVisibility(View.INVISIBLE);
break;
}
}
});
}
public void AdUpdate()
{
if(!this.adShown)
{
this.adShown = true;
this.adView = new AdView(this, AdSize.SMART_BANNER, MY_ID);
AdRequest adRequest = new AdRequest();
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
_mViewPager.addView(adView, params);
adView.loadAd(adRequest);
/*
Following code also didn't work for Admob banner ads. But it worked for appflood banner ads.
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.l1root);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(adView, params);
*/
}
}
///Code for Layout Fragment
public class LayoutOne extends Fragment
{
public static Fragment newInstance(Context context)
{
LayoutOne f = new LayoutOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}
}