I have been trying to compile an Android app but I am getting error saying that
java: non-static method
canGoBack()
cannot be referenced from a static context
java: non-static methodgoBack()
cannot be referenced from a static context
So basically I was trying to integrate airpush in this app but I am having with the goback()
function... I want it execute the airpush.startLandingPageAd();
on exiting the app (When there is no more history...
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);
}
}
You should add field
private WebView mWebView
and change onCreate into:
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);
}
so in function:
@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);
}
And it should works right now.
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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.