Ane*_*ess 2 java android android-fragments
嗨,我正在尝试在我的 Android 应用程序中实现原生广告,所以我有这个代码,您可以在 Github 中查看源代码。
它有时会在 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)
Run Code Online (Sandbox Code Playgroud)
执行此代码时发生错误:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
我们正在谈论这行代码
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.ad_unified, null);
Run Code Online (Sandbox Code Playgroud)
因此我的问题是: 在调用此函数 getLayoutInflater() 之前,我可以测试诸如实现条件之类的东西以确保 Fragment 附加到 Fragment Manager 吗?
| 归档时间: |
|
| 查看次数: |
792 次 |
| 最近记录: |