我是新來的Android我想開發一個瀏覽器應用程序,現在我正在嘗試開發它,在這裏我得到錯誤。我嘗試了另一種方法,也就是詢問您必須打開哪種瀏覽器,但對於我而言,我只想在我的應用中打開。我想開發瀏覽器應用程序,但我得到錯誤
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)
Afteredit
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
}
});
嗨,不需要後臺線程運行的WebView UI。它會在UI線程中執行 –
否,之後我想包括處理欄這就是爲什麼我是你那 – chandu
你應該重寫onPageFinished來跟蹤你的進度爲老丁 –