简体   繁体   中英

Crash when making Mopub ad banner visible

I noticed that sometimes have crashes in my Android app while displaying an ad layout using Mopub SDK. I've never been able to reproduce this issue and it never happened on my devices.

Here is the layout containing the ad banner:

<LinearLayout 
    android:id="@+id/layout_ads"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
>   

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/list_divider"
    />

    <com.mopub.mobileads.MoPubView
        android:id="@+id/adview"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/ad_height"
    />        
</LinearLayout>

Here is where the crash happens:

View adsLayout = null;
@Override
protected void onResume() {
  super.onResume();
  ...
  adsLayout = findViewById(R.id.layout_ads);
  ...
  if (adsLayout != null)
     adsLayout.setVisibility(View.GONE);
  ...
}

@Override
public void onBannerLoaded(MoPubView arg0) {
    if (adsLayout != null)
        adsLayout.setVisibility(View.VISIBLE);
}

And here is the error bothering me:

java.lang.NullPointerException
       at android.webkit.WebViewClassic.requestFocus(WebViewClassic.java:6997)
       at android.webkit.WebView.requestFocus(WebView.java:2044)
       at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2477)
       at android.view.ViewGroup.requestFocus(ViewGroup.java:2433)
       at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2477)
       at android.view.ViewGroup.requestFocus(ViewGroup.java:2433)
       at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2477)
       at android.view.ViewGroup.requestFocus(ViewGroup.java:2433)
       at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2477)
       at android.view.ViewGroup.requestFocus(ViewGroup.java:2433)
       at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2477)
       at android.view.ViewGroup.requestFocus(ViewGroup.java:2433)
       at android.view.View.requestFocus(View.java:6487)
       at android.view.View.requestFocus(View.java:6466)
       at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2609)
       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:708)
       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:708)
       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:708)
       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:708)
       at android.view.View.setFlags(View.java:8377)
       at android.view.View.setVisibility(View.java:5575)
       at myapp.MyActivity.onBannerLoaded(MyActivity.java:129)
       at com.mopub.mobileads.MoPubView.adLoaded(MoPubView.java:247)
       at com.mopub.mobileads.MoPubView.nativeAdLoaded(MoPubView.java:287)
       at com.mopub.mobileads.CustomEventBannerAdapter.onBannerLoaded(CustomEventBannerAdapter.java:100)
       at com.mopub.mobileads.GoogleAdMobBanner.onReceiveAd(GoogleAdMobBanner.java:170)
       at com.google.ads.internal.d.E(SourceFile:1180)
       at com.google.ads.internal.c$e.run(SourceFile:214)
       at android.os.Handler.handleCallback(Handler.java:615)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:213)
       at android.app.ActivityThread.main(ActivityThread.java:4788)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
       at dalvik.system.NativeStart.main(NativeStart.java)

Any idea about this issue?

Move adsLayout = findViewById(R.id.layout_ads); to onCreate(..) .

You are getting a null pointer because onResume(..) is not called before onBannerLoaded().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM