Lisa_14
Lisa_14

Reputation: 11

Webview in a Fragment Android Studio

I have a problem with a webview in a Fragment. When I execute the application the webview do not show the Html page. This is my code:

    public class BingMapFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private View main_view;

    private  static View v;
    private WebView webMapView;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    ListView lv;
    TraceAdapter aAdpt;
    private  FragmentChangeActivity parent;

    private OnFragmentInteractionListener mListener;

    public BingMapFragment() {
        // Required empty public constructor
         setRetainInstance(true);
    }



    // TODO: Rename and change types and number of parameters
    public static BingMapFragment newInstance(String param1, String param2) {
        BingMapFragment fragment = new BingMapFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

            webMapView = (WebView)main_view.findViewById(R.id.bingMapView);
            webMapView.setWebViewClient(new WebViewClient());

            WebSettings ws = webMapView.getSettings();
            ws.setJavaScriptEnabled(true);
            webMapView.loadUrl("file:///android_asset/prova.html");




        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        new DataSourceDeviceRealTime().getDeviceRealTime(this);
        main_view = inflater.inflate(R.layout.fragment_bing_map,container,false); 
        v=main_view;
        parent=(FragmentChangeActivity) getActivity();
        parent.setupActionBar(Constants.SCREEN_MAP);




    return inflater.inflate(R.layout.fragment_bing_map, container, false);

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    public void goBack() {
        if(GlobalData.isSalesforcePolicyActive() ||  GlobalData.isMobileRoutePolicyActive()){
            parent.switchContent(new MenuFragment(), true);
        } else {
            parent.logoutButtonClicked(); 
        }
        }
    }

I do not know the origin of the problem because I have already use the webview in another application. Thank you for reply.

Upvotes: 1

Views: 274

Answers (1)

convexHull
convexHull

Reputation: 1891

You are calling loadUrl() in onCreate method but the webview is inflated in onCreateView, which is called after onCreate - So this probably leads to some exception.

In your onCreateView method, you are inflating your layout twice - 1st you inflate the layout and find the WebView instance, but you are returning newly inflated layout. The returned layout is applied to fragment...

Put return main_view at the end of your onCreateView() instead of inflater.inflate...

webMapView = (WebView)main_view.findViewById(R.id.bingMapView); should also be called in onCreateView

Some other notes:

  • You should start the webview data loading in onStart or in onPause
  • to attach the activity you should use onAttach method

Upvotes: 1

Related Questions