我希望webview在按下后退按钮时显示上一页我使用.canGoBack来完成此操作但是" .canGoBack"和#34; .GoBack"下划线用红线表示====
"非静态方法' canGoBack()'无法从静态上下文引用"
任何帮助都是好的
继承我的主要活动java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import static in.notherstore.notherstore.R.id.webView;
import static in.notherstore.notherstore.R.id.webview;
public class MainActivity extends Activity {
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
WebView webView = new WebView(this);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.notherstore.in");
WebClientClass webViewClient = new WebClientClass();
webView.setWebViewClient(webViewClient);
setContentView(webView);
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Loading Store");
pd.setMessage("This may take several Minutes...");
pd.show();
}
public class WebClientClass extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
}
@Override
public void onBackPressed() {
if (WebView.canGoBack()) {
WebView.goBack();
return;
}
// Otherwise defer to system default behavior.
super.onBackPressed();
}
}

答案 0 :(得分:2)
您需要通过类webView
调用引用(WebView
)上的方法
所以应该
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
return;
}
// Otherwise defer to system default behavior.
super.onBackPressed();
}
并在webView
oncreate
声明
WebView webView;
OnCreate(..){
//..code
webView = new WebView(this);
//.. more code
}