setVisibility(View.GONE) 导致崩溃 [英] setVisibility(View.GONE) causes a Crash

查看:48
本文介绍了setVisibility(View.GONE) 导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在广告横幅上方有一个 Web 视图(ca-app-pub-3940256099942544/6300978111" 是测试广告 ID,因此我没有透露任何个人信息)

I have a Webview above a banner for an advert (the "ca-app-pub-3940256099942544/6300978111" is the test ad id, so I am not given out any personal info)

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

注意:Webview 中的 app:layout_constrain....我认为这可能是问题的一部分.

Note: the app:layout_constrain... in the Webview. I think that might be part of the problem.

在启动时,我正在检查购买情况.如果用户进行了任何购买,我会关闭带有代码的广告:

At start up, I am checking for purchases. If the user has made any purchases whatsoever, I turn off the ads with the code:

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

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

With the line adView.setVisibility(View.GONE); the program crashes with the unfounded allegation:

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); 行时,WebView 加载正常.

However, I know there is nothing wrong in the Webview, as when I hash out the line //adView.setVisibility(View.GONE);, the WebView loads fine.

有人知道为什么吗?

是否与 app:layout_constraint.. 有关,如果有,我该如何克服?

Is it anything to do with the app:layout_constraint.., and if so how do I overcome it?

推荐答案

好的,我找到了答案,不明显.它结合了@mTak 的答案和 Android 应用程序如何工作的关键部分(这是我没有意识到的,因为我来自 html/javascript 世界,而多线程是程序员在该环境中拥有绝对控制权的东西).

OK, I have found the answer, and it is not obvious. It is a combination of @mTak 's answer and a critical bit of how Android apps work (which is something I did not realise since I come from an html/javascript world and multi-threading is something programmers have absolute control over in that environment).

购买支票是在另一个线程上进行的.因此,尝试执行 adView.setVisibility(View.GONE) 给程序带来了痛苦.除了在主 UI 线程上,它似乎不喜欢你弄乱 UI.

The purchases checks were being made on another thread. So the attempt at doing adView.setVisibility(View.GONE) was causing grief to the program. It does not seem to like you messing with the UI except on the main UI thread.

这就是我更改代码以使 Android 协议满意的方式:

So this is how I changed my code to make the Android protocols happy:

我的购买检查(在单独的线程中,但在 MainActivity 中).注意,stringListARR 只是一个字符串数组,其中包含用户购买的产品代码.因此,如果至少有一次购买,无论是什么,我都决定关闭广告:

My Purchase check (in a separate thread, but in the MainActivity). Note, stringListARR is just an array of strings with the product codes that the user had purchased. So if there was at least one purchase, whatever it was, I had decided to turn off adverts:

// 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() 看起来像这样:

And @mTak was perfectly right how to do things properly. My turnAdvertsOff() looks like this:

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

所以感谢@mTak,也感谢谷歌上这个几乎找不到的帖子给了我这个想法:

So thanks to @mTak and thanks to this almost unfindable thread on google which gave me the idea:

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

事实上,如果 Stackoverflow 上的任何人都在回答有关当人们在 Android 中弄乱 UI 时崩溃的问题,这个 runOnUiThread 可能是第一个想到的解决方案.发现并不明显,也很痛苦.

In fact, if any people here on Stackoverflow are answering questions about crashes when people are messing with the UI in Android, this runOnUiThread might be the first solution that springs to mind. It is not obvious and a pain to discover.

这篇关于setVisibility(View.GONE) 导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆