Reputation: 579
I am trying to load a url into a webview. If there is internet connection, it should load it to the webview. Otherwise, a try again screen should be shown. In the try again screen, i have a try again button. If we click on it, the url should be loaded again. But with the code i've done, the app keeps crashing when i try to reload the url by clicking on try again button(even after i turn on net or if its still off). This is my code
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);
}
});
}
How can i reload the page on clicking on the try again button.
Upvotes: 1
Views: 1693
Reputation: 769
A bit of assumption here as there is no logcat:
EDIT Simple solution: create a listener class and pass the webView as a parameter to constructor so it knows what to reload.
class WebViewReloader implements View.OnClickListener{
private WebView browser = null;
public WebViewReloader(WebView target){
browser = target;
}
@Override
public void onClick(View view) {
tryAgainLayout.setVisibility(View.GONE);
browser.reload();
}
}
and then
tryAgainButton.setOnClickListener(new WebViewReloader(browser));
Other solutions: use a static reference to the WebView, or retrieve WebView by searching in its parent layout.
Upvotes: 2