gpt4 book ai didi

java - Android Webview 回退

转载 作者:行者123 更新时间:2023-11-29 05:38:10 31 4
gpt4 key购买 nike

我一直在尝试编译一个 Android 应用程序,但我收到错误提示

java: non-static method canGoBack() cannot be referenced from a static context
java: non-static method goBack() cannot be referenced from a static context

所以基本上我试图将 airpush 集成到这个应用程序中,但我正在使用 goback() 函数...我希望它执行 airpush.startLandingPageAd(); 退出应用程序时(当没有更多历史记录时......

package bizkit.app1;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.view.KeyEvent;
import com.djtjetyj.dghwdgh54656.Airpush;

public class FengShuiTips extends Activity {
private Airpush airpush;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("https://www.google.com");
Airpush airpush=new Airpush(getApplicationContext(), null);
airpush.startPushNotification(false);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && WebView.canGoBack()) {
WebView.goBack();
return true;
}
airpush.startLandingPageAd();
return super.onKeyDown(keyCode, event);
}
}

最佳答案

你应该添加字段

private WebView mWebView

并将 onCreate 更改为:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView)findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("https://www.google.com");
Airpush airpush=new Airpush(getApplicationContext(), null);
airpush.startPushNotification(false);
}

所以在函数中:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
airpush.startLandingPageAd();
return super.onKeyDown(keyCode, event);
}

它现在应该可以工作了。

关于java - Android Webview 回退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18685561/

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