0

I know the following is probably simple, but I can't seem to get it to work. It compile and runs, but doesn't load any URL I throw at it. I have Internet set in permisions, even checked to make sure it was running in some other code.

package com.richardmather.autoaccidentattorney;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class FindHospitalFragment extends Fragment {

    public FindHospitalFragment(){}

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

        View rootView = inflater.inflate(R.layout.fragment_find_hospital, container, false);
        final WebView webView = (WebView) rootView.findViewById(R.id.webView);
        String searchURL = "http://www.stackoverflow.com";
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("searchURL");

        return rootView;
    }
}

Here is the XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</LinearLayout>

I know it's something simple I'm missing.

EDIT: Took out the quotation marks, now it's loading the URL in Google Chrome....

1 Answers1

1

The problem probably is the line webView.loadURL("searchURL");.

By doing that it actually tries to load the URL searchURL and not the URL http://www.stackoverflow.com you saved in the variable searchURL.

So just remove the "s:

webView.loadURL(searchURL); // searchURL = "http://www.stackoverflow.com"

Edit:

To not launch chrome, simply add the following line:

webview.setWebViewClient(new WebViewClient());

Also see this question for details.

Community
  • 1
  • 1
i_turo
  • 2,529
  • 1
  • 11
  • 15
  • OK... well that's part of the problem... now it's pulling up Google Chrome with the URL? – user3690146 Aug 14 '14 at 15:52
  • Take a look a [this](http://stackoverflow.com/questions/7746409/android-webview-launches-browser-when-calling-loadurl) question. Basically just add `webView.setWebViewClient(new WebViewClient());`. – i_turo Aug 14 '14 at 16:01
  • It works now? I have edited my question to also include that part. Accept? – i_turo Aug 14 '14 at 16:13
  • webview.setWebViewClient(new WebViewClient()); Did the trick. – user3690146 Aug 14 '14 at 16:46