我有一个简单的webview来显示一个论坛..我需要“映射”后退按钮回到论坛的网页当然。我试着用这种方式但是给我一个致命的错误
public class MainActivity extends Activity
{
protected WebView webview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
if (savedInstanceState == null)
{
webview.loadUrl("http://www.proboards.com/");
}
}
@Override
protected void onSavedInstanceState(Bundle outState) {
//super.onSavedInstanceState(outState);
webview.saveState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true)
webview.goBack();
else finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
错误是onKeyDown第42行。
答案 0 :(得分:3)
更改此
WebView webview = (WebView) findViewById(R.id.webview);
// webview becomes local
要
webview = (WebView) findViewById(R.id.webview);
您已宣布
WebView webview;
作为班级成员
答案 1 :(得分:0)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true)
webview.goBack();
else finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
这是:How to go back to previous page if back button is pressed in WebView?
的副本在任何情况下,如果您处理了按键操作
,则必须返回true答案 2 :(得分:0)
试试这种方式
不要覆盖onKeyDown()方法
@Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
}else{
super.onBackPressed();
}
}