android – Can not resolve method findViewById(int)

android – Can not resolve method findViewById(int)

You need to do this in onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
   View view =  inflater.inflate(R.layout.secondefragment, container, false);
    mWebView = (WebView) view.findViewById(;
   progressBar = (ProgressBar) view.findViewById(;

   WebSettings webSettings = mWebView.getSettings();

   return view;


Fragment doesnt provide thefindViewById() method. This is provided in Activity or View. When implementing a Fragment you dont inflate your views in onCreate() (like you normally do in an Activity.) Instead, you do it in onCreateView() and you need to use the inflated root View to find the ID within the layout you inflated.

android – Can not resolve method findViewById(int)

getActivity().findViewById() works. However, this isnt a good practice because the fragment may be reused in another activity.

The recommended way for this is to define an interface.

  • The interface should contain methods by which the fragment needs to communicate with its parent activity.

    public interface MyInterfcae {
        void showTextView();
  • Then your activity implements that Interface.

    public class MyActivity extends Activity implements MyInterfcae {
        public void showTextView(){
  • In that fragment grab a reference to the interface.

    MyInterface mif = (MyInterface) getActivity();
  • Call a method.


This way, the fragment and the activity are fully decoupled and every activity which implements that fragment, is able to attach that fragment to itself.

Leave a Reply

Your email address will not be published.