Reputation: 740
This is my error;
Non-static method 'showInterstitial()' cannot be referenced from a static context
Note: Even If I make the interstitialAd as static, it won't show from the Fragment even if the ad has loaded.
I have the following public methods visible from onCreate
, and I call createInterstitial()
from there.
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()
works from the onCreate well. However, I want to display the ads when the user goes to one particular fragment in the PlaceholderFragment viewPager. But, I can't do that. Please tell me how to fix.
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;
}
Please note I can't make the PlaceHolderFragment static because I get the error;
"This fragment inner class should be static (com.xx.PlaceholderFragment) From the Fragment documentation: Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments()."
Upvotes: 0
Views: 1484
Reputation: 740
I solved this temporarily by creating a SECOND static interstitial ad object and calling the following code from the fragment.
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() {
}
});
Upvotes: 0