gpt4 book ai didi

安卓 : Simple web application crashes on links other then the http scheme

转载 作者:行者123 更新时间:2023-11-29 18:19:49 25 4
gpt4 key购买 nike

我是 Android 开发的初学者,如果这很明显,请原谅。我使用来自 android.com 的示例创建了一个简单的 Web 应用程序,但是,当单击 tel: 或 mailto: 等链接时,应用程序崩溃并要求我强制退出。我想知道是否有人可以告诉我原因。

public class MyApplication extends Activity {

WebView mWebView;

private class InternalWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost().equals("m.mydomain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}

}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* Use the WebView class to display website */
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new InternalWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.mydomain.nl/");
}

}

最佳答案

抱歉这么笨,这实际上解决了它。对于 mailto,没有主机,因此 getHost() 将返回 null 并导致异常。

private class InternalWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost() != null && Uri.parse(url).getHost().equals("m.domain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}

}
}

关于安卓 : Simple web application crashes on links other then the http scheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com