代码之家  ›  专栏  ›  技术社区  ›  Rewind

setVisibility(View.GONE)导致崩溃

  •  1
  • Rewind  · 技术社区  · 6 年前

    adView.setVisibility(View.GONE) 具有 adView.setVisibility(View.INVISIBLE) WebView加载了,但没有崩溃,所以当我删除AdView xml元素时,它看起来确实与此有关,而不仅仅是使其不可见。让它隐形并不理想,因为你会在广告的底部看到一个白色的空条。所以看起来它确实与重新加载Webview或修改UI有关。我的html/javascript代码是可靠的,可以处理任何维度更改。]

    我在广告横幅上方有一个网络视图 "ca-app-pub-3940256099942544/6300978111"

    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/adView" />
    
    <!-- "BANNER" or "LARGE_BANNER" -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
    

    注:在 app:layout_constrain... 在网络视图中。

    刚开始的时候,我在检查购买情况。如果用户有任何购买行为,我会关闭带有以下代码的广告:

    public void turnAdvertsOff() {
        advertsOn = false;
        AdView adView = (AdView) m_Context.findViewById(R.id.adView);
        adView.destroy();
        adView.setVisibility(View.GONE);
    }
    

    adView.setVisibility(View.GONE); 该程序因毫无根据的指控而崩溃:

    I/chromium: [INFO:CONSOLE(6381)] "Uncaught Error: Java exception was raised during method invocation", source: file:///android_asset/www/index.html?IsAndroidWebview=true (6381)
    D/WebView: loadUrl=about:blank
    D/WebView: destroy
    

    然而,我知道在Webview中没有什么问题,因为当我把行 //adView.setVisibility(View.GONE);

    有人知道为什么吗?

    这和 app:layout_constraint..

    3 回复  |  直到 6 年前
        1
  •  0
  •   user8959091 user8959091    6 年前

    这是建议隐藏adView的内容:

    adView.pause();
    adView.setVisibility(View.GONE); 
    
        2
  •  0
  •   Nirmal Code    6 年前

    尝试使用如下的线性布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1" />
    
    <!-- "BANNER" or "LARGE_BANNER" -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:adSize="BANNER"
        android:layout_gravity="center"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111" />
    </LinearLayout>
    

    智能横幅

        3
  •  0
  •   Rewind    6 年前

    购物支票是在另一条线上开的。所以尝试去做 adView.setVisibility(View.GONE)

    所以这就是我如何改变我的代码,使安卓协议高兴:

    我的购物支票(在单独的线程中,但在主活动中)。注意, stringListARR

    // Any purchase means we have no adverts
    myAdverts.advertsOn = stringListARR.size() > 0 ? false : true;
    // This is the evil line of code that caused the problem - this was being called not on the UI thread
    //if(!myAdverts.advertsOn) myAdverts.turnAdvertsOff();
    
    // And this is how to do it properly forcing it to be run on the UI thread
    // 'this' in the following is my MainActivity
    if(!myAdverts.advertsOn){
        this.runOnUiThread(new Runnable(){
            @Override
            public void run() {
                myAdverts.turnAdvertsOff();
            }
         });
    }
    

    @mTak完全正确地处理了事情。我的 turnAdvertsOff() 看起来像这样:

    public void turnAdvertsOff() {
        advertsOn = false;
        AdView adView = (AdView) m_Context.findViewById(R.id.adView);
        adView.pause();
        adView.setEnabled(false);
        adView.setVisibility(View.GONE);
    }
    

    所以感谢@mTak,感谢google上这个几乎不可理解的线程,它给了我这样一个想法:

    https://groups.google.com/forum/#!topic/google-admob-ads-sdk/d30EAC1zGFo
    

    runOnUiThread 可能是第一个想到的解决方案。这不明显,而且很难发现。