使用处理程序在webview片段android上实现后退按钮时出错

时间:2015-02-26 14:44:01

标签: android android-fragments webview

这是我的包含webview的片段代码,我正在尝试实现webview的后退按钮,而我的代码不起作用..在网上尝试了很多答案,但它似乎不起作用我的代码.. 基本上我在answer的帮助下使用处理程序尝试了它,我看到以下错误..

This is the error, which I'm getting when I compile it..

import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.app.Fragment;

import com.gururaju.verve15.R;
import java.util.logging.Handler;

public final class LoginFragment extends Fragment {

    public LoginFragment(){}

    static WebView mWeb;
    private View mContentView;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:{
                    webViewGoBack();
                }break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {

        mContentView = inflater.inflate(R.layout.fragment_login, null);
        mWeb = (WebView)mContentView.findViewById(R.id.webView);


        WebSettings settings = mWeb.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mWeb.getSettings().setBuiltInZoomControls(false);
        mWeb.loadUrl("http://example.com/login/");
        mWeb.setOnKeyListener(new OnKeyListener(){

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }
                return false;
            }

        });

    }

    private void webViewGoBack(){
        mWeb.goBack();
    }
}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

更改import java.util.logging.Handler;import android.os.Handler