Android WebView无法在一个活动中加载URL,但在另一个活动中加载

Android WebView无法在一个活动中加载URL,但在另一个活动中加载,android,webview,Android,Webview,我有一个列表视图,点击一个项目,一个新的活动被调用。所有活动都有一个webview,并在同一url中加载不同的url或不同的部分(同一url的不同div元素) 我面临的问题是,只有一个活动在webview中加载url,其余的显示一个空白的白色webview。这可能是Jsoup的问题吗 还要注意的是,当我只运行Jsoup代码来获取数据并输出到控制台时,它可以工作;但当我在活动中使用它时就不是了 下面是这两个活动的代码(它们只是在doc.select()中的一行中有所不同)。活动1工作正常,在web

我有一个列表视图,点击一个项目,一个新的活动被调用。所有活动都有一个webview,并在同一url中加载不同的url或不同的部分(同一url的不同div元素)

我面临的问题是,只有一个活动在webview中加载url,其余的显示一个空白的白色webview。这可能是Jsoup的问题吗

还要注意的是,当我只运行Jsoup代码来获取数据并输出到控制台时,它可以工作;但当我在活动中使用它时就不是了

下面是这两个活动的代码(它们只是在
doc.select()
中的一行中有所不同)。活动1工作正常,在webview中显示html,而活动2显示一个空白屏幕

活动1:

public class Article extends Activity {

ProgressDialog mProgressDialog;
WebView wv;

private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article);
    wv = (WebView) findViewById(R.id.webview1);

    new getHtml().execute();
}

private class getHtml extends AsyncTask<String, Void, Integer> {
    Elements tfa;

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Article.this);
        mProgressDialog.setTitle("Wikipedia");
        mProgressDialog.setMessage("Loading today's featured article...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            // Connect to the web site
            Document doc = Jsoup.connect(MainActivity.url).get();

            tfa = doc.select("div#mp-tfa");
            return SUCCESS;
        } catch (UnknownHostException e) {
            Log.e("Unknown Host Exception", "Network error", e);
            return NETWORK_ERROR;
        } catch (IOException e) {
            Log.e("IO Exception", "Failed to load HTML", e);
            return ERROR;
        } catch (Exception e) {
            Log.e("Exception", "An exception occured", e);
            return ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer result) {

        if (result == 2) {
            Toast.makeText(
                    getApplicationContext(),
                    "Network connection error. Check your internet connection and try again.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 3) {
            Toast.makeText(getApplicationContext(),
                    "Unknown error. Failed to load wikipedia.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 1) {
            wv.setWebViewClient(new WebViewClient());

            String tfa_html = tfa.outerHtml();
            tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
                    "a href=\"http://en.wikipedia.org/wiki/");
            tfa_html = tfa_html.replaceFirst(
                    "src=\"//upload.wikimedia.org/",
                    "src=\"http://upload.wikimedia.org/");
            wv.loadData(tfa_html, "text/html", null);
        }

        mProgressDialog.dismiss();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}
public class Article extends Activity {

ProgressDialog mProgressDialog;
WebView wv;

private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article);
    wv = (WebView) findViewById(R.id.webview1);

    new getHtml().execute();
}

private class getHtml extends AsyncTask<String, Void, Integer> {
    Elements tfa;

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Article.this);
        mProgressDialog.setTitle("Wikipedia");
        mProgressDialog.setMessage("Loading today's featured article...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            // Connect to the web site
            Document doc = Jsoup.connect(MainActivity.url).get();

            tfa = doc.select("div#mp-tfp");
            return SUCCESS;
        } catch (UnknownHostException e) {
            Log.e("Unknown Host Exception", "Network error", e);
            return NETWORK_ERROR;
        } catch (IOException e) {
            Log.e("IO Exception", "Failed to load HTML", e);
            return ERROR;
        } catch (Exception e) {
            Log.e("Exception", "An exception occured", e);
            return ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer result) {

        if (result == 2) {
            Toast.makeText(
                    getApplicationContext(),
                    "Network connection error. Check your internet connection and try again.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 3) {
            Toast.makeText(getApplicationContext(),
                    "Unknown error. Failed to load wikipedia.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 1) {
            wv.setWebViewClient(new WebViewClient());

            String tfa_html = tfa.outerHtml();
            tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
                    "a href=\"http://en.wikipedia.org/wiki/");
            tfa_html = tfa_html.replaceFirst(
                    "src=\"//upload.wikimedia.org/",
                    "src=\"http://upload.wikimedia.org/");
            wv.loadData(tfa_html, "text/html", null);
        }

        mProgressDialog.dismiss();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}
公共类文章扩展活动{
进程对话框;
网络视图;
私有静态最终整数成功=1;
专用静态最终int网络_错误=2;
私有静态最终整数错误=3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
wv=(WebView)findViewById(R.id.webview1);
新建getHtml().execute();
}
私有类getHtml扩展异步任务{
元素tfa;
@凌驾
受保护的void onPreExecute(){
mProgressDialog=新建ProgressDialog(Article.this);
设置标题(“维基百科”);
setMessage(“加载今天的特色文章…”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.show();
}
@凌驾
受保护的整数doInBackground(字符串…参数){
试一试{
//连接到网站
Document doc=Jsoup.connect(MainActivity.url).get();
tfa=文件选择(“div#mp tfa”);
回归成功;
}捕获(未知后异常e){
Log.e(“未知主机异常”,“网络错误”,e);
返回网络错误;
}捕获(IOE异常){
Log.e(“IO异常”,“加载HTML失败”,e);
返回误差;
}捕获(例外e){
Log.e(“异常”,“发生异常”,e);
返回误差;
}
}
@凌驾
受保护的void onPostExecute(整数结果){
如果(结果==2){
Toast.makeText(
getApplicationContext(),
“网络连接错误。请检查您的internet连接,然后重试。”,
Toast.LENGTH_LONG).show();
}否则如果(结果==3){
Toast.makeText(getApplicationContext(),
“未知错误。无法加载wikipedia。”,
Toast.LENGTH_LONG).show();
}else if(结果==1){
setWebViewClient(新的WebViewClient());
字符串tfa_html=tfa.outerHtml();
tfa\u html=tfa\u html.replaceAll(“a href=\”/wiki/”,
“a href=\”http://en.wikipedia.org/wiki/");
tfa_html=tfa_html.replaceFirst(
“src=\”//upload.wikimedia.org/”,
“src=\”http://upload.wikimedia.org/");
wv.loadData(tfa_html,“text/html”,空);
}
mProgressDialog.disclose();
}
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
//检查键事件是否为后退按钮,是否有历史记录
if((keyCode==KeyEvent.keyCode_BACK)&&wv.canGoBack()){
wv.goBack();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
}
活动2:

public class Article extends Activity {

ProgressDialog mProgressDialog;
WebView wv;

private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article);
    wv = (WebView) findViewById(R.id.webview1);

    new getHtml().execute();
}

private class getHtml extends AsyncTask<String, Void, Integer> {
    Elements tfa;

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Article.this);
        mProgressDialog.setTitle("Wikipedia");
        mProgressDialog.setMessage("Loading today's featured article...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            // Connect to the web site
            Document doc = Jsoup.connect(MainActivity.url).get();

            tfa = doc.select("div#mp-tfa");
            return SUCCESS;
        } catch (UnknownHostException e) {
            Log.e("Unknown Host Exception", "Network error", e);
            return NETWORK_ERROR;
        } catch (IOException e) {
            Log.e("IO Exception", "Failed to load HTML", e);
            return ERROR;
        } catch (Exception e) {
            Log.e("Exception", "An exception occured", e);
            return ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer result) {

        if (result == 2) {
            Toast.makeText(
                    getApplicationContext(),
                    "Network connection error. Check your internet connection and try again.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 3) {
            Toast.makeText(getApplicationContext(),
                    "Unknown error. Failed to load wikipedia.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 1) {
            wv.setWebViewClient(new WebViewClient());

            String tfa_html = tfa.outerHtml();
            tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
                    "a href=\"http://en.wikipedia.org/wiki/");
            tfa_html = tfa_html.replaceFirst(
                    "src=\"//upload.wikimedia.org/",
                    "src=\"http://upload.wikimedia.org/");
            wv.loadData(tfa_html, "text/html", null);
        }

        mProgressDialog.dismiss();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}
public class Article extends Activity {

ProgressDialog mProgressDialog;
WebView wv;

private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article);
    wv = (WebView) findViewById(R.id.webview1);

    new getHtml().execute();
}

private class getHtml extends AsyncTask<String, Void, Integer> {
    Elements tfa;

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Article.this);
        mProgressDialog.setTitle("Wikipedia");
        mProgressDialog.setMessage("Loading today's featured article...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            // Connect to the web site
            Document doc = Jsoup.connect(MainActivity.url).get();

            tfa = doc.select("div#mp-tfp");
            return SUCCESS;
        } catch (UnknownHostException e) {
            Log.e("Unknown Host Exception", "Network error", e);
            return NETWORK_ERROR;
        } catch (IOException e) {
            Log.e("IO Exception", "Failed to load HTML", e);
            return ERROR;
        } catch (Exception e) {
            Log.e("Exception", "An exception occured", e);
            return ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer result) {

        if (result == 2) {
            Toast.makeText(
                    getApplicationContext(),
                    "Network connection error. Check your internet connection and try again.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 3) {
            Toast.makeText(getApplicationContext(),
                    "Unknown error. Failed to load wikipedia.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 1) {
            wv.setWebViewClient(new WebViewClient());

            String tfa_html = tfa.outerHtml();
            tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
                    "a href=\"http://en.wikipedia.org/wiki/");
            tfa_html = tfa_html.replaceFirst(
                    "src=\"//upload.wikimedia.org/",
                    "src=\"http://upload.wikimedia.org/");
            wv.loadData(tfa_html, "text/html", null);
        }

        mProgressDialog.dismiss();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}
公共类文章扩展活动{
进程对话框;
网络视图;
私有静态最终整数成功=1;
专用静态最终int网络_错误=2;
私有静态最终整数错误=3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
wv=(WebView)findViewById(R.id.webview1);
新建getHtml().execute();
}
私有类getHtml扩展异步任务{
元素tfa;
@凌驾
受保护的void onPreExecute(){
mProgressDialog=新建ProgressDialog(Article.this);
设置标题(“维基百科”);
setMessage(“加载今天的特色文章…”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.show();
}
@凌驾
受保护的整数doInBackground(字符串…参数){
试一试{
//连接到网站
Document doc=Jsoup.connect(MainActivity.url).get();
tfa=文件选择(“div#mp tfp”);
回归成功;
}捕获(未知后异常e){
Log.e(“未知主机异常”,“网络错误”,e);
返回网络错误;
}捕获(IOE异常){
Log.e(“IO异常”,“加载HTML失败”,e);
返回误差;
}捕获(例外e){
Log.e(“异常”,“发生异常”,e);
返回误差;
}
}
@凌驾
受保护的void onPostExecute(整数结果){
如果(结果==2){
Toast.makeText(
getApplicationContext(),
“网络连接错误。请检查您的internet连接,然后重试。”,
Toast.LENGTH_LONG).show();
}否则如果(结果==3){
Toast.makeText(getApplicationContext(),
“未知错误。无法加载wikipedia。”,
Toast.LENGTH_LONG).show();
}else if(结果==1){
setWebViewClient(新的WebViewClient());
字符串tfa_html=tfa.outerHtml();
tfa\u html=tfa\u html.replaceAll(“a href=\”/wiki/”,
“a href=\”http://en.wikipedia.org/wiki/");
tfa_html=tfa_html.replaceFirst(