我一直在尝试编译Android应用,但我收到错误说
java:非静态方法
canGoBack()
不能从静态上下文中引用 java:无法从静态上下文引用非静态方法goBack()
所以基本上我试图在这个应用程序中集成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);
}
}
答案 0 :(得分:3)
你应该添加字段
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);
}
现在应该可行。
答案 1 :(得分:0)
public class testDemoActivity extends Activity
{
final Activity activity = this;
private WebView webview;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the BACK key and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://developer.android.com/index.html");
}
}