43964

Unable to start a WebView from an AsyncTask

Question:

I'd like to start a WebView from my AsyncTask but it doesn't seem to run. This is what my onPostExecute method looks like:

public class Viewer extends AsyncTask<URI, Integer, URI> { private Activity objContext = null; public Viewer(Activity objContext) { this.objContext = objContext; } protected void onPostExecute(URI uriWebpage) { WebView wbvBrowser = new WebView(this.objContext); wbvBrowser.getSettings().setBuiltInZoomControls(true); wbvBrowser.getSettings().setJavaScriptEnabled(true); wbvBrowser.loadUrl(uriWebpage.toString()); } }

My task is used by two activities in my application and therefore my global objContext variable is of type Activity. If I change the type of my objContext variable to the name of the calling class, it works fine but then I can't instantiate my task from the other calling class. I instantiate my task like this.

Viewer mytask = new Viewer(this);

How can I solve this?

Answer1:

Did you setContentView from original layout to webView or you have a container to put webview? otherwise I don't think webView UI appears.

Answer2:

Are you sure you execute your AsyncTask

@Override protected void onPostExecute(Void result){ } }.execute();

Also need to class name

Viewer mytask = new Viewer(YourclassName.this);

// or This may Helps you

private Context mcontext; public Viewer(Context objContext) { this.mcontext= objContext; } protected void onPostExecute(URI uriWebpage) { WebView wbvBrowser = new WebView(((Activity)(mcontext))); }

Answer3:

I have solved the issue. It has nothing to do with the use of Activity or Context. Both work just fine. Some URLs don't seem to load the WebView. I changed the URL of the WebView to point to a site like Google and it worked just fine. Seems like if the URL is incorrect, the WebView doesn't throw an exception but doesn't open up either.

Answer4:

I don't know why this happens, but why don't you just start the built in web browser through an Intent in your onPostExecute(Uri uriWebpage)?

Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uriWebPage); startActivity(intent);

If this solution doesn't satisfy you, then please post the error stack trace from LogCat.

Recommend

  • How to deal with duplicate methods in Go interface?
  • Android devices not having JIT
  • Can ffmpeg place Mp4 metainfo at the beginning of the file?
  • How can I use async/await while assigning a variable?
  • Getting an error serving images from App_Themes when using precompilation?
  • How to achieve density/heat map effect in iOS (iPhone/iPad)?
  • .Net how to set IsReadOnly for a property for a usercontrol
  • Retrieving custom entities in CRM 4 C#
  • QueryOver - add restriction on primitive collection
  • Interactive labeling of images in jupyter notebook
  • how can i close the resultSet, prepareStatement, conn in several methods below to avoid rs close and
  • Create One-To-One relationship between table and view in EF4?
  • Move elements from a listbox to another
  • Homomorphic filtering in OpenCV
  • Delete a ListItem by clicking the ImageView within the ListItem
  • Multiple canvases (pages) in Fabric.js
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • python selenium scraping tbody
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Swift: UIView.animate works unexpectedly
  • Thumbnails for mxml components in Flex
  • PHPMailer return to AJAX
  • How convert html to BBcode in C#
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • How to use Streams api peek() function and make it work?
  • Android: Unable to detect vertical plane
  • How to decleare char *const argv[] in swift [duplicate]
  • Debug `Unexpected end of JSON input Error` on content script
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Apple Mach-O Linker error (“duplicate symbol”)
  • How to get rgb from transparent pixel in js
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Grails - How to implement a foreign key relationship not using an id column?
  • Capture SIGFPE from SIMD instruction
  • Using Service Component Runtime
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication