2017-03-31 12 views
0

私は3つのタブを持つナビゲーションバーを持っています。各タブは断片です。 2番目のタブをクリックすると、インタースティシャル広告がポップアップします。 広告をどこにアップロードする必要がありますか?そして、私はどこに広告を見せなければならないのですか?インタースティシャル広告を表示するにはどうすればよいですか?

マイMainActivity:

public class MainActivity extends AppCompatActivity{ 
    public MediaPlayer mp; 
    NavigationView mNavigationView; 
    FragmentManager mFragmentManager; 
    FragmentTransaction mFragmentTransaction; 
    public InterstitialAd mInterstitialAd; 

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


     mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ; 

     mFragmentManager = getSupportFragmentManager(); 
     mFragmentTransaction = mFragmentManager.beginTransaction(); 
     mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit(); 



     android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 



     AdRequest adRequest = new AdRequest.Builder().build(); 

     AdRequest adRequest = new AdRequest.Builder().build(); 
     mInterstitialAd = new InterstitialAd(getContext()); 
     mInterstitialAd.setAdUnitId("ca-app-pub-9756227769488050/2954649723"); 
     mInterstitialAd.loadAd(adRequest); 


    } 

マイsecondFragment:

public class SecondFragment extends Fragment { 


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


     return rootView; 
    } 

と(私はそれが重要な場合は知らない)私のTabFragment:

public class TabFragment extends Fragment { 
    public static TabLayout tabLayout; 
    public static ViewPager viewPager; 
    public static int int_items = 3 ; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 


     /** 
     *Inflate tab_layout and setup Views. 
     */ 
      View x = inflater.inflate(R.layout.tab_layout,null); 
      tabLayout = (TabLayout) x.findViewById(R.id.tabs); 
      viewPager = (ViewPager) x.findViewById(R.id.viewpager); 

     /** 
     *Set an Apater for the View Pager 
     */ 
     viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 

     /** 
     * Now , this is a workaround , 
     * The setupWithViewPager dose't works without the runnable . 
     * Maybe a Support Library Bug . 
     */ 

     tabLayout.post(new Runnable() { 
      @Override 
      public void run() { 
        tabLayout.setupWithViewPager(viewPager); 
        } 
     }); 

     return x; 

    } 

    class MyAdapter extends FragmentPagerAdapter { 

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

     /** 
     * Return fragment with respect to Position . 
     */ 

     @Override 
     public Fragment getItem(int position) 
     { 

      if(position == 0){ 
       return new FirstFragment(); 
      } 
      if(position == 1){ 
       return new SecondFragment(); 
      } 
      if(position == 2){ 
       return new ThirdFragment(); 
      } 

     return null; 
     } 

     @Override 
     public int getCount() { 

      return int_items; 

     } 

     /** 
     * This method returns the title of the tab according to the position. 
     */ 

     @Override 
     public CharSequence getPageTitle(int position) { 

      switch (position){ 
       case 0 : 
        return "first"; 
       case 1 : 
        return "second"; 
       case 2 : 
        return "third"; 
      } 
       return null; 
     } 
    } 


     }); 
    } 

} 

だから私が言っているんshowInterstitial()

final public void showInterstitial(){ 
    mInterstitialAd.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      super.onAdLoaded(); 
      if(mInterstitialAd.isLoaded()){ 
       mInterstitialAd.show(); 
      } 

     } 

     @Override 
     public void onAdClosed() { 
     } 

この問題は1か月以上続いていますので、詳しく説明してください。ありがとう

+0

の可能性のある重複[この場合には、正しくインタースティシャル広告を表示する方法?](http://stackoverflow.com/questions/43141684/how-to-show-interstitial-ad-correctly-in-this-ケース) – William

答えて

0

メインアクティビティにload()を残しておくと、広告のキャッシュが開始されます。次に、アダプタのクリックイベントを処理する、つまりフラグメントを処理する場所であるshowInterstitial()関数を呼び出します。

+0

私はAndroidには新しく、どこで私のフラグメントを扱うのですか? ^^ –

0
public class MainActivity extends Activity { 
private InterstitialAd interstitial; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ; 

mFragmentManager = getSupportFragmentManager(); 
mFragmentTransaction = mFragmentManager.beginTransaction(); 
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit(); 

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 

    // Prepare the Interstitial Ad 
    interstitial = new InterstitialAd(MainActivity.this); 
    // Insert the Ad Unit ID 
    interstitial.setAdUnitId("ca-app-pub-123456789/123456789"); 

    //Locate the Banner Ad in activity_main.xml 
    AdView adView = (AdView) this.findViewById(R.id.adView); 

    // Request for Ads 
    AdRequest adRequest = new AdRequest.Builder().build(); 

    // Load ads into Banner Ads 
    adView.loadAd(adRequest); 


} 
public void displayInterstitial() { 
    // If Ads are loaded, show Interstitial else show nothing. 
    // Load ads into Interstitial Ads 
    interstitial.loadAd(adRequest); 

    // Prepare an Interstitial Ad Listener 
    interstitial.setAdListener(new AdListener() { 
     public void onAdLoaded() { 
      if (interstitial.isLoaded()) { 
     interstitial.show(); 
    } 
     } 
    }); 
    } 
} 
+0

もう一度私の質問をお読みください –

+0

私の編集された答えを見て&2番目のタブのクリックで 'displayInterstitial()'を呼び出してください –

関連する問題