-1

There is an error in the webview part. Please help me out. Following the tutorial here I have set up an app that should be able to open a URL and place it into an WebView. The only issue is that it cannot find findViewById code is here:

//webviewcode
    import android.app.Activity;
    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.WebSettings;
    import android.webkit.WebView;

    import info.healthazure.materialdesign.R;


    public class MessagesFragment extends Fragment {
        private WebView myWebView;
        public MessagesFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
            myWebView = (WebView)findViewById(R.id.webView);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myWebView.loadUrl("http://www.medplusmart.com/");



            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }

        @Override
        public void onDetach() {
            super.onDetach();
        }
    }

Any help for solving this issue would be much appreciated.

Anubhav Sarangi
  • 179
  • 1
  • 5
  • 15

2 Answers2

0

Instead of using just this findViewById() use rootView.findViewById() as your entire layout is inflated inside rootview.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
user2582324
  • 1,220
  • 1
  • 9
  • 10
0

Use

myWebView = (WebView)rootView.findViewById(R.id.webView);

instead of

myWebView = (WebView)findViewById(R.id.webView);
Passiondroid
  • 1,573
  • 1
  • 16
  • 28