0

I am trying to open a URL using WebView (positioned inside a Fragment) from AsyncTask.

Butterknife is being used to bind the view (WebView in this). The following code related to WebView is written inside doInBackground() of AsyncTask

mWebView.getSettings().setJavaScriptEnabled(false);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

However, on writing the code related to WebView, the IDE warns me with "Method getSettings() must be called from the UI thread, currently inferred thread is worker"

What does this mean? I am sorry, but can someone explain what's happening. Should I use something like runOnUiThread()?

Traveller
  • 157
  • 1
  • 3
  • 8
  • 1
    You cannot update UI or can't do any UI related task inside async task doInBackground method. Because this is a background thread. If you need to implement some UI task move it to onPostExecute of the async task there you can implement the same and your android application will not give you any exception. good Luck – Ankush Bist Jul 01 '16 at 04:59
  • @AnkushBist and Others - How using an AsyncTask is going to help me - if my code related to WebView needs to be moved to `onPostExecute()` which (as mentioned) runs on UI thread. Instead, why not post the WebView related code directly on UI thread? Just trying to get clarified. Some explanation will really help – Traveller Jul 01 '16 at 05:52
  • Basically first you need to know about a thread how it works and difference between a background thread and UI thread then all your doubts will be cleared. Read all the answer in the below link carefully...http://stackoverflow.com/questions/11758629/real-difference-between-asynctask-and-thread – Ankush Bist Jul 01 '16 at 06:12
  • Thanks for the link. However, I think this was not the answer I was expecting for. I mean, I know a little about UI threads, Background threads. My question was, (1) if there is a difference between running your code in `onCreate()` and `onPostExecute()` as both runs on UI thread? Why explicitly use an AsyncTask when there is nothing to run on background thread? – Traveller Jul 01 '16 at 07:11
  • yes there is no need to use async task if you are not running any code in background thread. But if you are running async task and at same you need to add some url at your webView it's better to add that code at your onPostExecute. – Ankush Bist Jul 01 '16 at 07:21

3 Answers3

2

You have to move mWebView.getSettings().setJavaScriptEnabled(false); inside your OnCreate() in runOnUiThread() method.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • I think `mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);` need to put outside doInBackground method instead of `setJavaScriptEnabled ` because this method is from `WebSettings` – ρяσѕρєя K Jul 01 '16 at 05:24
  • @ρяσѕρєяK I think it is better approch to put outside the `AsyncTask`. – Harshad Pansuriya Jul 01 '16 at 05:27
  • Got it. `doInBackground()` uses a background thread, `onPreExecute()` , `onProgressUpdate()`, `onPostExecute()` and `onCancelled()` uses the UI thread. How using an AsyncTask is going to help me - if my code related to WebView needs to be moved to `onPostExecute()` which (as mentioned) runs on UI thread. Instead, why not post the WebView related code directly on UI thread? Just trying to get clarified. Some explanation will really help. – Traveller Jul 01 '16 at 05:41
  • @Traveller glad to help you. – Harshad Pansuriya Jul 01 '16 at 05:42
  • A little explanation to my question would be helpful – Traveller Jul 01 '16 at 07:12
  • @Traveller means you are not understanding this thing Am I Right `"Method getSettings() must be called from the UI thread, currently inferred thread is worker"` – Harshad Pansuriya Jul 01 '16 at 07:21
1

Either write mWebView.getSettings().setJavaScriptEnabled(false); in runOnUiThread() or move it to onPostExecute()

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • Could you please explain, how "using the runOnUiThread() inside an ASyncTask to open an Url in WebView" different from "running a WebView to open an Url directly from UI thread"? – Traveller Jul 01 '16 at 05:31
0

You can't update the mWebView.getSettings().setJavaScriptEnabled(false); from a background Thread. You will need to do this in either onPostExecute() or OnCreate Method

Sachin
  • 1,307
  • 13
  • 23