Google Admob AD无法在Android的Fragment中显示

时间:2015-06-17 03:19:27

标签: android admob

我创建了一个Admob AD,它可以在我的其他布局中正确显示,现在我创建了一个Tabbed Activity,并将AD放在Fragment中,但AD无法显示,为什么?

谢谢!

PlaceholderFragmentOld.java

INSERT INTO work.sql_merged_database.dbo.TableA (ColumnA, ColumnB)
SELECT ColumnA, ColumnB FROM LinkedServerName.acs_merged_database.SchemaName.TableName

CleanupDelete.java

package ui.tab;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.support.v4.app.Fragment;
import android.widget.Toast;


import com.google.android.gms.ads.AdView;

import bll.PublicParFun;
import info.dodata.messagecleanup.R;

public class PlaceholderFragmentOld extends Fragment {

    private AdView adView;

    public PlaceholderFragmentOld() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.cleanup_delete_fragment_old, container, false);

        adView=(AdView)(rootView.findViewById(R.id.adView));
        SetAD(adView);

         return rootView;
    }

     public  void SetAD(AdView adView) {
        if (PublicParFun.IsRegistered == false) {
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .build();
            adView.loadAd(adRequest);
        }else{
            adView.setVisibility(View.GONE);
        }
    }

}

cleanup_delete.xml

package ui;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import info.dodata.messagecleanup.R;
import ui.tab.PlaceholderFragmentOld;
import ui.tab.PlaceholderFragmentPerson;
import ui.tab.PlaceholderFragmentReduce;
import ui.tab.PlaceholderFragmentTrim;

public class CleanupDelete extends ActionBarActivity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cleanup_delete);

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }


    //--------------------------------------------------------------------------------------------

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;
            switch(position){
                case 0:
                    fragment = new PlaceholderFragmentOld();
                    break;
                case 1:
                    fragment = new PlaceholderFragmentReduce();
                    break;
                case 2:
                    fragment = new PlaceholderFragmentTrim();
                    break;
                case 3:
                    fragment = new PlaceholderFragmentPerson();
                    break;
                default:
                    throw new IllegalArgumentException("Invalid section number");
            }

            return fragment;
        }



        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {           
            return null;
        }
    }


}

cleanup_delete_fragment_old.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
    android:layout_width="match_parent" android:layout_height="match_parent"
/>

日志

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_ui" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:paddingTop="20dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:orientation="vertical" >


        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/ad_unit_id" />

        <TextView
            android:id="@+id/tVDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dip"
            style="@style/myTextDesciption"
            android:text="This is a descript"/>


    </LinearLayout>


    <LinearLayout
        android:id="@+id/linearLayoutToolBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="4" >


        <Button
            android:id="@+id/btnClose"
            style="@style/myTextMedium"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/BtnClose" />

    </LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果您使用的是ViewPager,则只需在片段的父片段上使用xml和Java代码的一段必要的Admob代码即可。根据您的个人经验,您不需要在单独的片段上使用,这会带来更多问题。只需在父母身上使用