Question

Im using webview which loading webpage and then every 5s reload this page. Page is loaded OK but after 5s when should start reloading I get this:

Especially at line 48 where is method: wvNovaMenzaCammera.reload();

 01-31 21:01:06.198  26188-26361/com.pfittech.menzauniza E/AndroidRuntime﹕ FATAL EXCEPTION: Timer-0
    java.lang.RuntimeException: java.lang.Throwable: Warning: A WebView method was called on thread 'Timer-0'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
            at android.webkit.WebView.checkThread(WebView.java:2093)
            at android.webkit.WebView.reload(WebView.java:955)
            at com.pfittech.menzauniza.CommunityFragment$1$1.run(CommunityFragment.java:48)
            at java.util.Timer$TimerImpl.run(Timer.java:284)
     Caused by: java.lang.Throwable: Warning: A WebView method was called on thread 'Timer-0'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.
            at android.webkit.WebView.checkThread(WebView.java:2084)
            at android.webkit.WebView.reload(WebView.java:955)
            at com.pfittech.menzauniza.CommunityFragment$1$1.run(CommunityFragment.java:48)
            at java.util.Timer$TimerImpl.run(Timer.java:284)

There is my code:

public class CommunityFragment extends Fragment {
WebView wvNovaMenzaCammera;
private static final String TAG = "Cammera 1";
private ProgressDialog progressBarWV1;
public CommunityFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_community, container, false);
    wvNovaMenzaCammera = (WebView)rootView.findViewById(R.id.wvCammera1);
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            WebSettings webSettings = wvNovaMenzaCammera.getSettings();
            wvNovaMenzaCammera.getSettings().setJavaScriptEnabled(true);
            wvNovaMenzaCammera.loadUrl("http://zuzo.sk/ftp/akt-a.jpg");
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    wvNovaMenzaCammera.reload();
                }
            }, 5000, 5000);
        }
    });
    return rootView;
}
Was it helpful?

Solution

Change your TimerTask to the following:

new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            public void run() {
                wvNovaMenzaCammera.reload();
            }
        });      
    }
}

As the error log says, you have to reload the WebView in the UI thread, and your TimerTask runs in a background thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top