问题
I was making a webview application for my local network. I have two static IPs. All I want to do is to load second if 1st one is down. (Because one of them is always running).
What I'm doing is to check it in onRecievedError()
function and change the URL value, then call the onCreate()
again like this.
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
url1="http://192.168.43.XXX"; //here XXX is used just to hide my IP from public
onCreate(new Bundle());
}
the onCreate()
method has to render the new URL but my application closes itself upon this call. Why is this so?
Please help because I'm a beginner.
回答1:
use this code
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
if(failingUrl.equals(url1))
{
//you can use load url if fail first url
// dont't use onCreate again
webview.loadUrl(url2);
}
}
来源:https://stackoverflow.com/questions/57351203/load-second-url-if-first-is-not-present-in-android-webview