1

我一直在尝试编译一个 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);
    }
}
4

2 回答 2

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);
    }

它现在应该可以工作。

于 2013-09-08T15:58:21.600 回答
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");
    }
}
于 2013-09-08T16:44:30.433 回答