0
我想加载到web视图的网址。如果有互联网连接,它应该加载到web视图。否则,应该显示再试一次屏幕。在再试一次屏幕中,我有一个再试一次的按钮。如果我们点击它,则应该再次加载该网址。但随着我做的代码,应用程序不断崩溃,当我尝试重新加载URL通过点击再试一次按钮(即使我打开网络或如果它仍然关闭)。这是我的代码打开互联网后重新加载webview
tryAgainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tryAgainLayout.setVisibility(View.GONE);
browser = (WebView)findViewById(R.id.webView1);
open(browser);
}
});
private void open(View view){
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setAppCacheEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.getSettings().setSupportMultipleWindows(true);
browser.setWebViewClient(new MyBrowser());
browser.setWebChromeClient(new MyCustomChromeClient());
mContext=this.getApplicationContext();
browser.loadUrl(target_url);
MainActivity.this.progressBar.setProgress(0);
browser.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
如何在重新加载页面时单击再次尝试按钮。
我也试过这样做。但它仍然崩溃。在logcat中,我为浏览器(Webview对象)获取一个空指针例外 – 2014-12-06 14:53:42
您必须确保使用corect布局/视图搜索web视图(调用findViewById)。你可以在Button视图上调用findViewById,它总是返回null。我会用提案编辑我的答案。 – 2014-12-06 15:07:55
看起来像个好主意。但该应用程序仍然崩溃。我想我在某个地方犯了一些错误。这似乎是答案。 – 2014-12-06 15:43:24