ANR only when using "setVisibility"

297 views Asked by At

this is my first android app, and I have the following problem with this code:

package com.example.andbro;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

        String myURL;
        WebView browser;
        ProgressBar progressBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                this.myURL = "http://www.google.com/";
                this.browser = (WebView) findViewById(R.id.webView1);
                this.progressBar = (ProgressBar) findViewById(R.id.progressBar1);

                Button rel = (Button) findViewById(R.id.button1);
                browser.setWebChromeClient(new WebChromeClient() {

                        @Override
                        public void onProgressChanged(WebView view, int newProgress) {

                                super.onProgressChanged(view, newProgress);
                                progressBar.setProgress(newProgress);

                                if (newProgress == 100) {
                                        progressBar.setVisibility(View.GONE);
                                } else {
                                        progressBar.setVisibility(View.VISIBLE);
                                }

                        }
                });

                browser.getSettings().setJavaScriptEnabled(true);
                browser.loadUrl(myURL);

                rel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                browser.reload();
                        }
                });

        }
}

It is a simple Webview that loads a webpage ( Google ), with a reload Button and a ProgressBar.

So while the page is loading, I check if the progress in onProgressChanged is 100 or lower than 100: if it is 100, I hide the progress with:

progressBar.setVisibility(View.GONE);

else

progressBar.setVisibility(View.VISIBLE);

Hovewer my app freeze and then it ends showing me an ANR message. Just commenting those two lines, and everything works ( obviously the progress bar won't hide in that case ).

It doesn't crash if i put, for example, two Toasts like "start" or "end" after the if/else statement.

Why is this happening only with setVisibility?

Also this is the logcat log for both status( with or without those lines )

11-20 12:01:13.979: W/ApplicationPackageManager(29061): getCSCPackageItemText()
11-20 12:01:13.979: I/PersonaManager(29061): getPersonaService() name persona_policy
11-20 12:01:14.019: V/WebViewChromium(29061): Binding Chromium to the background looper Looper (main, tid 1) {429e2bf8}
11-20 12:01:14.019: I/chromium(29061): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
11-20 12:01:14.019: I/BrowserProcessMain(29061): Initializing chromium process, renderers=0
11-20 12:01:14.029: W/chromium(29061): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
11-20 12:01:14.039: W/ApplicationPackageManager(29061): getCSCPackageItemText()
11-20 12:01:14.039: I/Adreno-EGL(29061): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
11-20 12:01:14.039: I/Adreno-EGL(29061): OpenGL ES Shader Compiler Version: E031.24.00.08+13
11-20 12:01:14.039: I/Adreno-EGL(29061): Build Date: 03/28/14 Fri
11-20 12:01:14.039: I/Adreno-EGL(29061): Local Branch: 0328_AU200_patches
11-20 12:01:14.039: I/Adreno-EGL(29061): Remote Branch: 
11-20 12:01:14.039: I/Adreno-EGL(29061): Local Patches: 
11-20 12:01:14.039: I/Adreno-EGL(29061): Reconstruct Branch: 
11-20 12:01:14.109: D/ProgressBar(29061): setProgressDrawable drawableHeight = 0
11-20 12:01:14.209: D/OpenGLRenderer(29061): Enabling debug mode 0
11-20 12:01:14.219: D/ProgressBar(29061): updateDrawableBounds: left = 0
11-20 12:01:14.219: D/ProgressBar(29061): updateDrawableBounds: top = 0
11-20 12:01:14.219: D/ProgressBar(29061): updateDrawableBounds: right = 1080
11-20 12:01:14.219: D/ProgressBar(29061): updateDrawableBounds: bottom = 30
11-20 12:01:14.219: D/ProgressBar(29061): updateDrawableBounds: mProgressDrawable.setBounds()
11-20 12:01:14.229: W/AwContents(29061): nativeOnDraw failed; clearing to background color.
11-20 12:01:14.279: W/AwContents(29061): nativeOnDraw failed; clearing to background color.
11-20 12:01:14.339: I/chromium(29061): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
11-20 12:01:14.379: I/chromium(29061): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
0

There are 0 answers