【问题标题】:Android - Displaying WebView in FragmentAndroid - 在片段中显示 WebView
【发布时间】:2016-09-15 07:48:42
【问题描述】:

所以我在 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="com.example.dwinnbrown.myapplicationv20.MainFragment">

    <!-- TODO: Update blank fragment layout -->
    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

我现在正在尝试通过与片段关联的 java 文件将 url 加载到 web 视图中。但是我遇到了一个错误“无法解析 findViewByID(int)”。有什么想法吗?

这是我尝试用于片段的代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);


        String url = "http://winn-brown.co.uk/";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);
    }



}

【问题讨论】:

    标签: java android android-fragments webview


    【解决方案1】:

    做这样的事

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    
        String url = "http://winn-brown.co.uk/";
        WebView view = (WebView) rootView.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);
    
        return rootView ;
    }
    

    【讨论】:

    • 嗯错误“视图已在范围内定义”。它在这条线上失败了:WebView view = (WebView) view.findViewById(R.id.webView);
    • 我已更新代码请尝试更新代码。
    • 谢谢。您也可以进行投票,这将不胜感激:)
    【解决方案2】:

    下面是片段的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebActivity">
    
    <WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/webview">
    </WebView>
    
    </RelativeLayout>
    

    在片段类中试试这个

    public class MainFragment extends Fragment {
    
    WebView webView;
    
    public MainFragment() {
    
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        webView=(WebView)getActivity().findViewById(R.id.webview);
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    
        webView.loadUrl("your_url");
    
        webView.setWebViewClient(new Mybrowser());
    
        if (savedInstanceState != null)
            webView.restoreState(savedInstanceState);
        else
            webView.loadUrl("your_url");
    }
    
    private class Mybrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
    }
    

    【讨论】:

    • 请用文字描述解决方案
    • 我已更新代码请尝试使用此更新代码
    【解决方案3】:
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_main, container, false);
    
    
            String url = "http://winn-brown.co.uk/";
            WebView wv = (WebView) view.findViewById(R.id.webView);
            wv.setWebViewClient(new WebViewClient());
            wv.getSettings().setJavaScriptEnabled(true);
            wv.loadUrl(url);
        }
    

    【讨论】:

    • 永远无法设置 webview,因为之前有返回
    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2012-12-30
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多