I am new to Android I want to develop a browser app now I am trying to develop it, in this I am getting errors. I tried in another method also on that it's asking on which browser you have to open like that but for me I want to open in my app only.
WebviewActivity.java
package com.example.admin.fristapp;
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends Activity {
WebView webview1;
// String url = "http://www.google.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webview1 = (WebView) findViewById(R.id.webview1);
// webview1.loadUrl(url);
//MyBrowser my ;
new Background().execute();
}
public class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public class Background extends AsyncTask<String,String,String>
{
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings)
{
search();
return null;
}
}
public void search()
{
webview1.setWebViewClient(new WebviewActivity.MyBrowser());
String url = "http://www.google.com";
webview1.getSettings().setJavaScriptEnabled(true);
webview1.loadUrl(url);
}
}
logcat
java.lang.Throwable: A WebView method was called on thread 'AsyncTask #4'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {180a245b} called on null, FYI main Looper is Looper (main, tid 1) {180a245b})
at android.webkit.WebView.checkThread(WebView.java:2290)
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends Activity {
WebView webview;
String url = "http://www.google.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webview =(WebView )findViewById(R.id.webview1);
//WebView(WebviewActivity.this);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Do your code when page finished
}
});
Your are loading a url in WebView
in doInBackground()
method. You should do on the UI thread.
loadUrl()
is already doing everything asynchronously. So no need to of AsyncTask
. Move your code in onCreate()
method.
If you want to display a loading indicator, you need to use as below :
WebView webview = new WebView(MainActivity.this);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Do your code when page finished
}
});
webview.loadUrl(url);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.