2017-12-29 78 views
0

これは私のエラーです。静的フラグメントからAdmobインタースティシャル広告を表示するにはどうすればよいですか?

Non-static method 'showInterstitial()' cannot be referenced from a static context 

注:私はstaticとしてInterstitialAdにを作る場合でも、それは広告が読み込まれた場合でも破片から表示されません。

onCreateから公開されている公開方法はありますが、createInterstitial()に電話します。

public void crateInterstitial(){ 

     interstitialAd = new InterstitialAd(getApplicationContext()); 
     interstitialAd.setAdUnitId(MyID); 
     interstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdLoaded() { 
       // not call show interstitial ad from here 
      } 

      @Override 
      public void onAdClosed() { 
       loadInterstitial(); 
      } 
     }); 
     loadInterstitial(); 
    } 

    public void loadInterstitial(){ 
     AdRequest interstitialRequest = new AdRequest.Builder() 
       .addTestDevice(MyTestDevice) 
       .build(); 
     interstitialAd.loadAd(interstitialRequest); 
     Log.e("Loading another","Ad"); 
    } 

    public void showInterstitial(){ 
     if (interstitialAd.isLoaded()){ 
      interstitialAd.show(); 
      Log.e("Showing","Ad"); 
     } 
     else{ 
      loadInterstitial(); 
      Log.e("Loading","Ad"); 
     } 

    } 

showInterstitial()は、onCreateからうまく機能します。しかし、ユーザーがPlaceholderFragment viewPagerの特定のフラグメントに移動したときに広告を表示したいと思います。しかし、私はそれをすることはできません。修正方法を教えてください。

 public static class PlaceholderFragment extends Fragment { 
      /** 
      * The fragment argument representing the section number for this 
      * fragment. 
      */ 
      private static final String ARG_SECTION_NUMBER = "section_number"; 

      public PlaceholderFragment() { 
      } 

      /** 
      * Returns a new instance of this fragment for the given section 
      * number. 
      */ 
      public static PlaceholderFragment newInstance(int sectionNumber) { 
       PlaceholderFragment fragment = new PlaceholderFragment(); 
       Bundle args = new Bundle(); 
       args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
       fragment.setArguments(args); 
       return fragment; 
      } 

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

       showInterstitial(); //This is where the error is. 

       View rootView = inflater.inflate(R.layout.fragment_results, container, false); 
       return rootView; 
       } 

エラーが発生したため、PlaceHolderFragmentを静的にすることはできません。

"フラグメントの内部クラスは静的(com.xx.PlaceholderFragment)である必要がありますフラグメントのドキュメント:すべてのフラグメントには空のコンストラクタが必要です。そのため、アクティビティの状態を復元するときにインスタンス化できます。代わりに、引数はsetArguments(Bundle)で呼び出し元から提供され、後でgetArguments()でFragmentによって取得されるため、パラメータを持つ他のコンストラクタはありません。

答えて

0

私は、SECOND静的インタースティシャル広告オブジェクトを作成し、フラグメントから次のコードを呼び出すことで、これを一時的に解決しました。

public static InterstitialAd sinterstitialAd; 

sinterstitialAd = new InterstitialAd(getContext()); 
         sinterstitialAd.setAdUnitId(MY_AD_UNIT_ID); 
         final AdRequest sinterstitialRequest = new AdRequest.Builder() 
           .addTestDevice(MY_TEST_DEVICE) 
           .build(); 
         sinterstitialAd.loadAd(sinterstitialRequest); 
         Log.e("sinterstitial Ad", "Loading"); 

sinterstitialAd.setAdListener(new AdListener() { 
          @Override 
          public void onAdLoaded() { 
           if (sinterstitialAd.isLoaded()) { 
            sinterstitialAd.show(); 
            Log.e("Showing", "New Ad"); 
           } 
          } 

          @Override 
          public void onAdClosed() { 

          } 
         }); 
関連する問題