Reputation: 670
Hi I am trying to implement native Ads in my Android App so I have this code , you can check the source code in Github.
It does sometimes fire this error in Logcat :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.august_themes.free_fast_stock_inventory_manager, PID: 3234
java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.
at androidx.fragment.app.Fragment.getLayoutInflater(Fragment.java:1503)
at androidx.fragment.app.Fragment.onGetLayoutInflater(Fragment.java:1452)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1484)
The error happens when this code is executed :
private void refreshAd() {
refresh.setEnabled(false);
AdLoader.Builder builder = new AdLoader.Builder(getContext(), getResources().getString(R.string.AdmobNativeAdsID));
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
v.findViewById(R.id.nativeads_adplaceholder);
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.ad_unified, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
We are talking about this line of code
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.ad_unified, null);
Hence my question : Can I test something like implement condition to make sure that the Fragment is attached to the Fragment Manager before calling this function getLayoutInflater() ?
Upvotes: 2
Views: 1523
Reputation: 601
For your question according to Android Developers fragment has .isAdded()
function that Return true if the fragment is currently added to its activity.
hope it helps.
Upvotes: 8