java - Webview 无法在 fragment 中工作

标签 java android android-activity webview fragment

我在选项卡 Activity fragment 中有一个 WebView 。但是,我的代码只产生一个空白页。该代码在常规 Activity 中确实有效。

Main2Activity.java(选项卡 Activity )

 public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

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

        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
            View rootView = inflater.inflate(R.layout.fragment_subpage1, container, false);
            return rootView;

        } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
            View rootView = inflater.inflate(R.layout.fragment_subpage2, container, false);
            return rootView;

        } else {
            View rootView = inflater.inflate(R.layout.fragment_subpage3, container, false);
            return rootView;
        }
    }
}

subpage2.java

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
final WebView browser = (WebView) getView().findViewById(R.id.webView);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_subpage1, container, false);

    browser.getSettings().setJavaScriptEnabled(true);
    browser.setWebViewClient(new WebViewClient() {
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                browser.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                        return false;
                    }
                });
            } else {
                browser.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        return false;
                    }
                });
            }
        }
    });
    browser.loadUrl("www.example.com");

    return v;
}

和fragment_subclass.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.subpage2">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView" />

最佳答案

像这样初始化 WebView :

View v = inflater.inflate(R.layout.fragment_subpage1, container, false);
final WebView browser = (WebView) v.findViewById(R.id.webView);

试试这个:在网址前面添加http

browser.getSettings().setJavaScriptEnabled(true); 
browser.loadUrl("http://www.example.com");   //add http at front  
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setLoadWithOverviewMode(true);

在 list 中您必须添加权限:

<uses-permission android:name="android.permission.INTERNET" />

关于java - Webview 无法在 fragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42042408/

相关文章:

java - Java中如何获取父类的属性?

java - 排序插入位置

java - 如何从 pc 将当前日期设置为 jDateChooser?

java - 服务类空指针

跨多个 Activity 的标题栏中的 Android 加载图标

Android服务服务崩溃2次,停止: ServiceRecord

java - 如何从 url 的最后一个路径中分割剩余路径

android - 如何延迟为 kotlin 中的 val 赋值?

android - GeoCoder getFromLocation 方法会导致抛出 NetworkOnMainThreadException 吗?

android - OnCreate() 与 OnStart()