2017-12-29 70 views
0

이것은 내 오류입니다.정적 조각에서 Admob 삽입 광고를 표시하는 방법은 무엇입니까?

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

참고 : 나는 정적으로 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를 정적으로 만들 수 없습니다.

"조각의 내부 클래스는 static (com.xx.PlaceholderFragment)이어야합니다. 조각에서 모든 조각에는 빈 생성자가 있어야하므로 활동 상태를 복원 할 때 인스턴스화 할 수 있습니다. 하위 클래스는 프래그먼트가 다시 인스턴스화 될 때 이러한 생성자가 호출되지 않으므로 매개 변수가있는 다른 생성자가 없으며 호출자가 setArguments (Bundle)를 사용하여 인수를 제공하고 나중에 getArguments()를 사용하여 Fragment에서 인수를 가져올 수 있습니다.

답변

0

두 번째 정적 삽입 광고 개체를 만들고 조각에서 다음 코드를 호출하여 일시적으로이 문제를 해결했습니다.

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() { 

          } 
         });