制作Mopub广告横幅时可见崩溃

时间:2014-01-27 08:15:35

标签: android android-layout mopub

我注意到,在使用Mopub SDK显示广告布局时,我的Android应用中有时会崩溃。 我从来没有能够重现这个问题,它从未发生在我的设备上。

以下是包含广告横幅的布局:

<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>

这是发生崩溃的地方:

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);
}

这就是困扰我的错误:

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)

关于这个问题的任何想法?

1 个答案:

答案 0 :(得分:0)

移动  adsLayout = findViewById(R.id.layout_ads);  到onCreate(..)

您收到空指针,因为在onResume(..)

之前未调用onBannerLoaded().