android 当加载MaxNative广告到recyclerView它给出错误“Spicified视图已经有父.所以你应该调用removeView对父的引用?

fdx2calv  于 2023-04-28  发布在  Android
关注(0)|答案(1)|浏览(113)

我正在编写代码从Applovins加载MaxNative广告到recyclerView。我第一次加载它并在每次新调用时使用它。下面是代码

fun showNativeAd2(adContainer: FrameLayout) {
        val params = adContainer.layoutParams
        params.height = 300.px

        adContainer.layoutParams = params
        if (isPremium()) {
            adContainer.visibility = View.GONE
            return
        }
        adContainer.visibility = View.VISIBLE
        val nativeAdLoader = MaxNativeAdLoader(adMobIds.nativeId, adContainer.context)

        nativeAdLoader.setNativeAdListener(object : MaxNativeAdListener() {

            override fun onNativeAdLoaded(nativeAdView: MaxNativeAdView?, ad: MaxAd) {
                // Clean up any pre-existing native ad to prevent memory leaks.
                if (nativeAd != null) {
                    nativeAdLoader.destroy(nativeAd)
                }
                nativeAd = ad
                globalNativeAd = nativeAdView

                adContainer.removeAllViews()
                adContainer.addView(nativeAdView)
            }

            override fun onNativeAdLoadFailed(adUnitId: String, error: MaxError) {
                // We recommend retrying with exponentially higher delays up to a maximum delay
                log("$adUnitId  $error")
            }

            override fun onNativeAdClicked(ad: MaxAd) {
                log("$ad")
            }
        })
        if (globalNativeAd == null)
            nativeAdLoader.loadAd()
        else{
            adContainer.removeAllViews()
            adContainer.addView(globalNativeAd)
        }
    }

这里globalNative是全局变量,如果globalNative为null,则在第一次调用函数时只初始化一个,对于每次新调用,它只显示存储在globalNative变量中内容。
这里是StackTrace。

2023-04-27 12:15:29.952 10265-10265/com.thesrb.bluewords E/CustomActivityOnCrash: The previous app process crashed. This is the stack trace of the crash:
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5269)
        at android.view.ViewGroup.addView(ViewGroup.java:5090)
        at android.view.ViewGroup.addView(ViewGroup.java:5030)
        at android.view.ViewGroup.addView(ViewGroup.java:5003)
        at com.thesrb.bluewords.ads_libs.AppLovinUtils.showNativeAd2(AppLovinUtils.kt:161)
        at com.thesrb.bluewords.adapter.WordAdapter.setAds(WordAdapter.kt:217)
        at com.thesrb.bluewords.adapter.WordAdapter.onBindViewHolder(WordAdapter.kt:188)
        at com.thesrb.bluewords.adapter.WordAdapter.onBindViewHolder(WordAdapter.kt:32)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
lo8azlld

lo8azlld1#

我已经创建了一个扩展函数(在Kotlin中)来避免这样的崩溃,它对我来说非常有效。
将此函数添加到任何文件或Object类型类中

fun ViewGroup.addCleanView(view: View?) {
    (view?.parent as? ViewGroup)?.removeView(view)
    this.removeAllViews()
    this.addView(view)
}

现在,用法简单如下:

adContainer.addCleanView(globalNativeAd)

这里也是,

adContainer.addCleanView(nativeAdView)

不需要删除视图等。我的扩展功能将处理所有。

相关问题