The code:
private fun initAds() {
val adRequest = PublisherAdRequest.Builder().build()
pavDetail.loadAd(adRequest)
pavDetail.adListener = object : AdListener() {
override fun onAdLoaded() {
super.onAdLoaded()
pavDetail.visibility = View.VISIBLE
}
override fun onAdFailedToLoad(i: Int) {
pavDetail.visibility = View.GONE
super.onAdFailedToLoad(i)
}
}
}
pavDetail
is com.google.android.gms.ads.doubleclick.PublisherAdView
and I get it by kotlinx synthetic from XML layout.
This code sometimes throws IllegalStateException
because pavDetail
mysteriously becomes null in onAdLoaded
& onAdFailedToLoad
.
I made it pavDetail?.visibility
to prevent the crash but I am not sure this will work because I am suspicuous that calling pavDetail
itself may throw that exception. Remember this is not NullPointerException
this is a IllegalStateException
giving message pavDetail must not be null
.
Hence, I ask why pavDetail
becomes null and how to prevent the crash.