基本的webview应用程序问题

时间:2015-01-04 13:59:55

标签: android webview

我正在尝试创建一个基本的webview应用程序,但它提供了4个错误。

  

错误:(35,9)错误:方法不会覆盖或实现方法   来自超类

     

错误:(46,32)错误:非静态方法canGoBack()不能   从静态上下文引用

     

错误:(47,32)错误:无法引用非静态方法goBack()   来自静态背景

     

错误:任务':app:compileDebugJava'的执行失败。   编译失败;有关详细信息,请参阅编译器错误输出。

有谁能告诉我我的代码有什么问题?

这是我的代码:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        setContentView(R.layout.activity_main);

        WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.loadUrl("http://facebook.com/");
    }

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (WebView.canGoBack()) {
                        WebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
    }
}

这是我的新代码,现在你的帮助显示没有错误,但仍然无效(当我尝试在模拟器中运行时,无法完成gradle执行):

public class MainActivity extends ActionBarActivity {
    private WebView mainWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        setContentView(R.layout.activity_main);

        mainWebView = (WebView) findViewById(R.id.mainWebView);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.loadUrl("http://facebook.com/");
    }

    private class MyCustomWebViewClient extends WebViewClient {

        public boolean shouldOvrrideLoading (WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mainWebView.canGoBack()) {
                        mainWebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

错误实际上解释了什么是错误的:

" 非静态方法goBack()无法从静态上下文引用"

您需要将静态WebView.canGoBack()替换为您实例化的网络视图mainWebView.canGoBack()

同样适用于WebView.goBack()

答案 1 :(得分:-1)

mainWebView 变量应该是类 MainActivity

的对象
public class MainActivity extends ActionBarActivity {
        private WebView mainWebView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            setContentView(R.layout.activity_main);

            mainWebView = (WebView) findViewById(R.id.mainWebView);

            WebSettings webSettings = mainWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            mainWebView.setWebViewClient(new MyCustomWebViewClient());
            mainWebView.loadUrl("http://facebook.com/");
        }

        private class MyCustomWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        if (mainWebView.canGoBack()) {
                            mainWebView.goBack();
                        } else {
                            finish();
                        }
                        return true;
                }
            }
        }
    }