【问题标题】:Webview not working in fragmentWebview 在片段中不起作用
【发布时间】:2017-02-04 16:00:53
【问题描述】:

我在标签活动片段中有一个 web 视图。但是,我的代码只产生一个空白页。该代码在常规活动中确实有效。

Main2Activity.java(选项卡活动)

 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" />

【问题讨论】:

    标签: java android android-activity webview fragment


    【解决方案1】:

    像这样初始化 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);
    

    在清单中你必须添加权限:

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

    【讨论】:

    • 完成了所有这些,没有任何区别。不幸的是仍然是一个空白页:/
    • 尝试使用不同的网址...另请参阅此答案:stackoverflow.com/questions/29560783/…
    • 不同的网址不起作用。该答案有类似的问题,但我没有任何其他与 youtube 视频有关的活动。我已经无数次检查了 webview 代码,它在其他活动中也能正常工作。我认为还有其他问题,但我不知道是什么。
    【解决方案2】:

    通过在没有来自 android studio 的不必要代码的情况下重新执行我的标签片段来解决它。这次我确实喜欢这个tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2017-09-03
      • 2020-02-03
      • 2014-10-02
      • 2021-01-03
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多