【问题标题】:how to load failingUrl in webview again?如何再次在 webview 中加载 failedUrl?
【发布时间】:2015-03-22 06:43:30
【问题描述】:

如何在 webview 中再次加载 failedUrl ??

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webView); //webview in activity_main.xml
[....] // some other codes
web.loadUrl("http://my-example-site.com");
[....] // some other codes
}
// if no network, then show error.xml with a reload button;
@Override
public void onReceivedError(final WebView view, int errorCode, String description, final String failingUrl){
setContentView(R.layout.error);
button = (Button) findViewById(R.id.button1); // Reload Button in error.xml
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
web.loadUrl(failingUrl); //this one is not working...
}
});
}

我想再次加载 failedUrl..

【问题讨论】:

    标签: android webview


    【解决方案1】:

    试试这个代码

    @Override
    public void onReceivedError(final WebView view, int errorCode, String description, final String failingUrl)
    {
        setContentView(R.layout.error);
        button = (Button) findViewById(R.id.button1); // Reload Button in error.xml
        button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            web.reload();
        }
        });
    }
    

    你必须在初始化 webview 之前添加这个语句

    web.setWebViewClient(new WebViewClient());
    

    【讨论】:

    • 尝试使用 view.reload() 而不是 web.reload()
    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2023-03-23
    相关资源
    最近更新 更多