1)您WebView
和ProgressBar
其内部Fragment
的布局和您使用的inflater
初始化和return
你的片段布局
2.)因此,片段布局的每个视图都在rootView
返回的内部inflater
,您必须使用它rootView
来初始化属于您的其他视图Fragment
3.)使用mwebView
变量而不是创建局部变量view
4.)shouldOverrideUrlLoading
什么都不做,所以添加view.load
并返回true
public class Home_Fragment extends Fragment { public WebView mwebView; ProgressBar bar; // ^^^ declare bar public Home_Fragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_home_, null); bar = (ProgressBar) rootView.findViewById(R.id.progressBar); // initialize bar mwebView = (WebView) rootView.findViewById(R.id.webView); mwebView.loadUrl("https://mbasic.facebook.com"); mwebView.getSettings().setJavaScriptEnabled(true); mwebView.setWebViewClient(new MyWebViewClient()); return rootView; } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageStarted(final WebView view, final String url, final Bitmap favicon) { bar.setVisibility(View.VISIBLE); // ^^^ use it as it is super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { bar.setVisibility(View.GONE); // ^^^ use it as it is super.onPageFinished(view, url); } } }