如何在片段文件上添加使用网络视图?

How to add use web view on a fragment file?

如何在我的片段上调用 ​​WebView
如您所见,我调用了它,但出现了错误。我想要实现的是一个在我的应用程序上打开的网页。

这是我使用的代码:

package sampleapp.razen.com.sampleapp;


import android.app.FragmentTransaction;
import android.os.Bundle;
import android.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 GeoNjoroFragment extends Fragment {


    public GeoNjoroFragment() {

        // Required empty public constructor
    }



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

        View view = inflater.inflate(R.layout.fragment_geo_njoro, container, false);

        WebView myWebView = (WebView)view.findViewById(sampleapp.razen.com.sampleapp.R.id.webview);
        myWebView.getSettings().setLoadsImagesAutomatically(true);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setSupportZoom(true);
        myWebView.loadUrl("http://www.google.com/");
// Inflate the layout for this fragment
        return view;

    }



}

这是我收到的错误图片:

试试这个代码。 首先你必须膨胀你的视图然后使用 findViewById() 方法。 将此权限 uses-permission android:name="android.permission.INTERNET" 添加到 Androidmanifest.xml 文件。

    View view = inflater.inflate(R.layout.fragment_geo_njoro, container, false);

    WebView myWebView = (WebView)view.findViewById(sampleapp.razen.com.sampleapp.R.id.webview);
        myWebView.getSettings().setLoadsImagesAutomatically(true);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setSupportZoom(true);
        myWebView.loadUrl("http://www.hivisasa.com/wb/nakuru/news/113249");
// Inflate the layout for this fragment
return view;

你不绑定视图导致webview找不到要绑定的视图,应该是:

视图view = inflater.inflate(R.layout.fragment_geo_njoro,容器,false); WebView myWebView = (WebView)view.findViewById(sampleapp.razen.com.sampleapp.R.id.webview);