是的,我搜索过该网站并没有找到可以帮助我的内容 因为每个人都说使用" shouldOverrideUrlLoading" 但那并没有帮助我 - 它不起作用 我试图打开 - 在同一页面中一个应该打开新页面链接的URL,但是当我按下它时 - 没有任何反应 我将它用作片段
public class FlightFragment extends Fragment {
private WebView webView;
public FlightFragment(){};
private String currentUrl;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.flight_fragment,container,false);
currentUrl = "https://flights.travelor.me/?locale=he";
webView = (WebView)rootView.findViewById(R.id.flightweb);
WebViewClient MyWebViewClient= new WebViewClient();
MyWebViewClient.shouldOverrideUrlLoading(webView, currentUrl);
ourWebViewClient(currentUrl);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new FlightFragment.MyBrowser());
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == keyEvent.ACTION_DOWN) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return false;
}
});
webView.loadUrl(currentUrl);
return rootView;
}
public void ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
private class MyBrowser extends WebViewClient {
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.equals(currentUrl)) {
view.loadUrl(request.getUrl().toString());
}
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
}
}
答案 0 :(得分:0)
当您的网址被截获时,您将返回false,请尝试按此更改
webView.setWebViewClient(new WebViewClient() {
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
Intent intent=new Intent(CurrentActivity.this,NewActivity.class);
intent.putExtra("url to load in NewActivity",url);
startActivity(intent); //get this url using getextra in new activity
}
}
}});
答案 1 :(得分:0)
如果您的目标版本是> = 26 然后覆盖下面的函数
23-Feb-2018 14:02:05.036 SEVERE [http-apr-8082-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [spring] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.hibernate.hql.internal.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:562)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4540)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1242)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4252)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2429)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1320)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3877)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1923)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1873)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1873)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1851)
因为自Android N以来不推荐@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
。在第一个覆盖方法中编写Android N及以上的代码。
在您的代码中删除此
shouldOverrideUrlLoading(WebView view, String url)
当您在此代码下再次设置WebViewClient并更改MyBrowser类
时 webView.setWebViewClient(new WebViewClient() {
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
return false;
}
view.loadUrl(url);
return true;
}
}});
答案 2 :(得分:0)
@golan Try to make your Webview fragment as a Activity which will work like a generic view and will open web pages as per requirement based on input url.
shouldOverrideUrlLoading loading method will required if you want to check the input urls.
Do all required web settings like
WebSettings webSettings = webviewBinding.detailWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadWithOverviewMode(false);
webSettings.setUseWideViewPort(true);
webSettings.setSupportZoom(true);
Override onBackPressed() method with following code:
if (detailWebView.canGoBack()) {
detailWebView.goBack();
}
else {
super.onBackPressed();
}
The canGoBack() will allow you to go back upto all web pages and if no more pages to go back it will redirect to you to last loaded activity or view.