本文介绍了安卓的WebView canGoBack()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面()的教程,但我有(浏览网页历史记录)我乳宁它在我的设备上,我点击一个链接,但是当我点击后退按钮关闭应用程序问题:
这是我的活动:
公共类MainActivity延伸活动{@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 的WebView myWebView =(的WebView)findViewById(R.id.webView1);
myWebView.setWebViewClient(新MyWebViewClient());
myWebView.setWebViewClient(新WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(真);
myWebView.loadUrl(http://www.google.com);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}私有类MyWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
如果(Uri.parse(URL).getHost()。等于(www.example.com)){
//这是我的网站,所以不要覆盖;让我的WebView加载页面
返回false;
}
//否则,该链接不上我的网站页面,因此启动该处理URL另一活动
意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL));
startActivity(意向);
返回true;
}
}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
的WebView myWebView = NULL;
//检查关键事件是后退按钮,如果有历史
如果((键code == KeyEvent.KEY code_BACK)及和放大器; myWebView.canGoBack()){
myWebView.goBack();
返回true;
}
//如果不是返回键或没有网页的历史,泡到默认
//系统行为(可能退出该活动)
返回super.onKeyDown(键code,事件);
}
}
解决方案
公共类MainActivity延伸活动{
的WebView myWebView;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); myWebView =(的WebView)findViewById(R.id.webView1);
myWebView.setWebViewClient(新MyWebViewClient());
myWebView.setWebViewClient(新WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(真);
myWebView.loadUrl(http://www.google.com);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}私有类MyWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
如果(Uri.parse(URL).getHost()。等于(www.example.com)){
//这是我的网站,所以不要覆盖;让我的WebView加载页面
返回false;
}
//否则,该链接不上我的网站页面,因此启动该处理URL另一活动
意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(URL));
startActivity(意向);
返回true;
}
}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
//检查关键事件是后退按钮,如果有历史
如果((键code == KeyEvent.KEY code_BACK)及和放大器; myWebView.canGoBack()){
myWebView.goBack();
返回true;
}
//如果不是返回键或没有网页的历史,泡到默认
//系统行为(可能退出该活动)
返回super.onKeyDown(键code,事件);
}
I'm following (Building Web Apps in WebView) tutorial, but I have problem with (Navigating web page history) I'm runing it on my device and I click on a link but when I click the back button the application shuts down:
That's my activity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// 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) {
WebView myWebView = null;
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
解决方案
public class MainActivity extends Activity {
WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// 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) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
这篇关于安卓的WebView canGoBack()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!