clamum
clamum

Reputation: 1364

Android - WebView is Blank

This is my first time working with the WebView widget and I've done searching via Google and on StackExchange and cannot find a solution to my problem.

I have placed a WebView on a FragmentActivity, loaded by a main Activity, and it is just blank no matter if I use "http://www.google.com" as a URL or if I specify a local .html file (very simple one containing a couple paragraphs, header, and link). It is not shown below, but my AndroidManifest.xml has the INTERNET permission (and EXTERNAL_STORAGE).

activity_main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:name="com.adamtcarlson.websiteviewer.MainActivityFragment"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <FrameLayout
        android:id="@+id/frm_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java:

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

    public class MainActivity extends FragmentActivity {

    /**
     * The Activity's tag.
     */
    private static final String Tag = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.v(Tag, "onCreate()");

        populateContentFragment(false);
    }

    private void populateContentFragment(boolean addToBackStack) {
        Log.v(Tag, "populateContentFragment()");

        Uri uri = Uri.parse("http://www.google.com/");
        //Uri uri = Uri.parse("file:///data/app/testpage.html");
        Fragment fragment = WebBrowserFragment.newInstance(uri);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
                .replace(R.id.frm_content, fragment);

        if (addToBackStack) {
            fragmentTransaction.addToBackStack(Tag);
        }

        fragmentTransaction.commit();
    }
}

fragment_webbrowser.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="#AAAAAA">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wbvBrowser">
    </WebView>

</RelativeLayout>

WebBrowserFragment.java:

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * A placeholder fragment containing a simple view.
 */
public class WebBrowserFragment extends Fragment {

    private WebView wbvBrowser = null;
    private String browserUri = null;

    /**
     * The Activity's tag.
     */
    private static final String Tag = "WebBrowserFragment";

    public WebBrowserFragment() {
        Bundle args = getArguments();

        if (args != null) {
            browserUri = args.getString("Uri");
        }
    }

    public static WebBrowserFragment newInstance(Uri uri) {
        Log.v(Tag, "newInstance(). Uri: " + uri.toString());

        WebBrowserFragment fragment;

        if (uri != null) {
            fragment = new WebBrowserFragment();

            Bundle args = new Bundle();
            args.putString("Uri", uri.toString());

            fragment.setArguments(args);
        }
        else {
            fragment = new WebBrowserFragment();
        }

        return fragment;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.v(Tag, "onCreateView()");

        View rootView = inflater.inflate(R.layout.fragment_webbrowser, container, false);

        wbvBrowser = (WebView) rootView.findViewById(R.id.wbvBrowser);

        wbvBrowser.getSettings().setJavaScriptEnabled(true);

        wbvBrowser.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        wbvBrowser.loadUrl(browserUri);

        return rootView;
    }
}

I should note in the Fragment that I tried commenting out the .setWebViewClient() call but that also did nothing. I got the original code from a book I have, "Android Programming: The Big Nerd Ranch Guide." I can't see any errors or anything that looks out-of-place on the logcat log. I've tried viewing the app via the emulator, using API 10 and 19. I've also tried installing the .apk on my LG Ultimate 2 and all show a blank screen (I changed the background to the Fragment's RelativeLayout to gray to ensure the WebView was showing up).

Much thanks for any assistance.

Upvotes: 0

Views: 820

Answers (1)

Derek Fung
Derek Fung

Reputation: 8211

wbvBrowser.loadUrl(browserUri);

browserUri is not set

simply add browserUri = getArguments().getString("Uri"); before the line.

i.e.

browserUri = getArguments().getString("Uri");
wbvBrowser.loadUrl(browserUri);

Upvotes: 1

Related Questions