无法解决方法' getLayoutInflater()' - Android Java

时间:2015-07-05 20:41:17

标签: java android

我希望在我的webView - 为youtube视频中添加全屏模式。我发现例如https://stackoverflow.com/a/16179544/5070495。我已经按照所有步骤,调整代码,但我有一个问题。

我对rootView使用layout,但我不知道在getLayoutInflater()之前如何使用它。

问题是:

View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments

所有代码:

public class vod extends Fragment {
    private WebView wv7;
    public static final String TAG = "VOD";
    private VideoEnabledWebView webView;
    private VideoEnabledWebChromeClient webChromeClient;


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


        View rootView = inflater.inflate(R.layout.vod, container, false);
        // Set layout
        //rootView.findViewById(R.layout.vld);

        // Save the web view
        wv7 = (VideoEnabledWebView) rootView.findViewById(R.id.webView7);

        // Initialize the VideoEnabledWebChromeClient and set event handlers
        View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout); // Your own view, read class comments
        ViewGroup videoLayout = (ViewGroup) rootView.findViewById(R.id.videoLayout); // Your own view, read class comments
        View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
        webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
        {
            // Subscribe to standard events, such as onProgressChanged()...
            @Override
            public void onProgressChanged(WebView view, int progress) {
                // Your code...
            }
        };
        webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() {
            @Override
            public void toggledFullscreen(boolean fullscreen) {
                // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
                if (fullscreen) {
                    WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getActivity().getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14) {
                        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                    }
                } else {
                    WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getActivity().getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14) {
                        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }

            }
        });
        wv7.setWebChromeClient(webChromeClient);

        // Navigate everywhere you want, this classes have only been tested on YouTube's mobile site
        wv7.loadUrl("http://m.youtube.com");


    }
    public void onBackPressed()
    {
        // Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it
        if (!webChromeClient.onBackPressed())
        {
            if (wv7.canGoBack()) {
                wv7.goBack();
            }
        }
    }
}

感谢您的帮助:p

1 个答案:

答案 0 :(得分:1)

你为什么要调用getLayoutInflater?你有参数inflater 改变它:

View loadingView = inflater.inflate(R.layout.view_loading_video, null); // Your own view, read class comments

编辑: 第二个解决方案:

View loadingView = getActivity().getLayoutInflater().inflate(R.layout.view_loading_video, null);

但我认为第一种解决方案更好。