gpt4 book ai didi

javascript - webview .canGoBack 显示为错误(红色)

转载 作者:行者123 更新时间:2023-12-01 02:58:29 30 4
gpt4 key购买 nike

我希望 webview 在按下后退按钮时显示上一页我正在使用 .canGoBack 来完成此操作,但“.canGoBack”和“.GoBack”带有红线下划线,声明 ====

“无法从静态上下文引用非静态方法‘canGoBack()’”

任何帮助都会很好

这是我的主要 Activity 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();
}
}

最佳答案

您需要通过类 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
}

关于javascript - webview .canGoBack 显示为错误(红色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46589304/

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