WebViewFragment webView is null after doing a FragmentTransaction
V

4

13

I currently have my application set up with a ListFragment on the left and a DetailsFragment on the right (similar to the layout on the tablet below).

layout

On the details fragment (fragment next to the list) I have a goto deal button, which when pressed should replace the detailsFragment with a WebViewFragment.

The problem I am having is that when trying to load a url in the webviewfragment the WebView is null.

WebViewFragment webViewFragment = new WebViewFragment();

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.deal_details_fragment, webViewFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

// Set the url
if (webViewFragment.getWebView()==null)
    Log.d("webviewfragment", "is null");
webViewFragment.getWebView().loadUrl("http://www.google.com");

Below is my main layout which has the original two fragments defined.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/main_activity_layout"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

  <fragment
      android:name="com.bencallis.dealpad.DealListFragment"
      android:id="@+id/deal_list_fragment"
      android:layout_weight="1"
      android:layout_width="0px"
      android:layout_height="match_parent" >
      <!-- Preview: layout=@layout/deal_list_fragment -->
  </fragment>
  <fragment
      android:name="com.bencallis.dealpad.DealDetailsFragment"
      android:id="@+id/deal_details_fragment"
      android:layout_weight="2"
      android:layout_width="0px"
      android:layout_height="match_parent" >
      <!-- Preview: layout=@layout/deal_details_fragment -->
  </fragment>

</LinearLayout>

It seems that the webViewFragment is not being created fully as the WebView has not been initialised. I have looked online but there is very little information regarding the WebViewFragment.

Any ideas how to ensure WebView is initialised in the WebViewFragment?

Vadose answered 6/2, 2012 at 13:58 Comment(2)
Please post the code for your DealWebViewFragment class.Pehlevi
@Pehlevi - Sorry my DealWebViewFragment was just a recreating of WebViewFragment. I have change the above code back to WebViewFragment (the same problem exists).Vadose
V
12

With great help from Espiandev I have managed to get a working WebView. To ensure that links opened in the fragment and not in a web browser application I created a simple InnerWebView client which extends WebViewClinet.

public class DealWebViewFragment extends Fragment {

    private WebView mWebView;
    private boolean mIsWebViewAvailable;
    private String mUrl = null;

    /**
     * Creates a new fragment which loads the supplied url as soon as it can
     * @param url the url to load once initialised
     */
    public DealWebViewFragment(String url) {
        super();
        mUrl = url;
    }

    /**
     * Called to instantiate the view. Creates and returns the WebView.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        if (mWebView != null) {
            mWebView.destroy();
        }
        mWebView = new WebView(getActivity());
        mWebView.setOnKeyListener(new OnKeyListener(){


            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                  if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                        mWebView.goBack();
                        return true;
                    }
                    return false;
            }

        });
        mWebView.setWebViewClient(new InnerWebViewClient()); // forces it to open in app
        mWebView.loadUrl(mUrl);
        mIsWebViewAvailable = true;
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        return mWebView;
    }

    /**
     * Convenience method for loading a url. Will fail if {@link View} is not initialised (but won't throw an {@link Exception})
     * @param url
     */
    public void loadUrl(String url) {
        if (mIsWebViewAvailable) getWebView().loadUrl(mUrl = url);
        else Log.w("ImprovedWebViewFragment", "WebView cannot be found. Check the view and fragment have been loaded.");
    }

    /**
     * Called when the fragment is visible to the user and actively running. Resumes the WebView.
     */
    @Override
    public void onPause() {
        super.onPause();
        mWebView.onPause();
    }

    /**
     * Called when the fragment is no longer resumed. Pauses the WebView.
     */
    @Override
    public void onResume() {
        mWebView.onResume();
        super.onResume();
    }

    /**
     * Called when the WebView has been detached from the fragment.
     * The WebView is no longer available after this time.
     */
    @Override
    public void onDestroyView() {
        mIsWebViewAvailable = false;
        super.onDestroyView();
    }

    /**
     * Called when the fragment is no longer in use. Destroys the internal state of the WebView.
     */
    @Override
    public void onDestroy() {
        if (mWebView != null) {
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }

    /**
     * Gets the WebView.
     */
    public WebView getWebView() {
        return mIsWebViewAvailable ? mWebView : null;
    }

    /* To ensure links open within the application */
    private class InnerWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }


    }

Hopefully this is useful to someone in the future.

Vadose answered 9/2, 2012 at 17:5 Comment(0)
F
7

EDIT: So I played around with this for a while and it seems that the WVF is a bit rubbish and designed to be overridden. However, there's no documentation on this at all! The problem stems from the fact you can call getWebView() before the Fragments view is loaded, hence your NullPointerException. Except there isn't any way to detect when the Fragment's view has been loaded, so you're kind of stuck!

Instead I overrode the class, adding bits and changing bits, so that now it will work fine. Check this link for the code. Then instead of using:

WebViewFragment webViewFragment = new WebViewFragment();

to load your Fragment, use:

ImprovedWebViewFragment wvf = new ImprovedWebViewFragment("www.google.com");

This class also includes a convenience method for loading a url, that won't throw an Exception if there's no WebView.

So, no, I don't think there's a particularly simple way for using the built-in WebViewFragment, but it is pretty easy to make something that works instead. Hope it helps!

Forensic answered 8/2, 2012 at 16:16 Comment(9)
I am using the built in Android WebViewFragment [link] developer.android.com/reference/android/webkit/… . I can look into making my own but surely I should be able the built in one.Vadose
oh yeah, sorry didn't realise that. I'll have a fiddle with the code you've posted and see if I can help.Forensic
Thank you. Did you have any luck?Vadose
Thanks Espiandev. I guess I will have to override the WebViewFragment. Thanks also for the code. I have based my final solution of this. One problem with it was that links would try and open in the default web browser application on the device. I have managed to solve this fairly easily with an inner class. One other problem I am now having is how to best show a progression bar to the user for the webpage.Vadose
The WebView documentation (developer.android.com/reference/android/webkit/WebView.html) describes a WebChromeClient which can give you the progress of loading the page, maybe you could use this?Forensic
A better approach would have been to supply arguments for launching fragments instead of supplying parameters for the constructor. When you overload the constructor you end up with issues later when the system restores your fragments. You should use the static instantiate methode or empty constructor for creating fragments.Bessbessarabia
@Bessbessarabia I'm not disagreeing but would like to know... Android assumes that certain components implement constructors with specific signatures, but is there really any harm in providing additional constructors for when a user builds an interface programmatically? As long as a Fragment still implements the necessary constructors and can save and restore its state via the normal mechanisms, what's the harm?Photometry
@Photometry well it depends. If the parameters are necessary for the fragment to run it would be a bad approach as it would give nullpointerexception. Also it is the recommended approach from Google so it is a good idea to follow in order to stay compatible with updates etc.Bessbessarabia
"... it seems that the WVF is a bit rubbish ..." - tsk, tsk, that's a bit harsh, considering that @CommonsWare Mark Murphy uses it extensively in his book "The Busy Coder's Guide to Android Development". But he does use it as you imply, always deriving some other class from WebViewFragment.Thermography
E
2

WebViewFragment as is is not that straightforward to use. Try this simple extension (You can copy/paste):

public class UrlWebViewFragment extends WebViewFragment{

    private String url;

    public static UrlWebViewFragment newInstance(String url) {
        UrlWebViewFragment fragment = new UrlWebViewFragment();
        fragment.url = url;
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        WebView webView = (WebView) super.onCreateView(inflater, container, savedInstanceState);
        webView.loadUrl(url);
        return webView;
    }         
  }

Call where you need using the factory method:

WebViewFragment fragment = UrlWebViewFragment.newInstance("http://ur-url.com");
Exotic answered 26/10, 2014 at 15:23 Comment(1)
Fragment with constructor without bundle. It's not a good sample for use.Jordans
R
0

Fragments can only be replaced if they were initiallized in Java, not XML. I think so, I had the same problem and it solved it. Change your XML to this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/main_activity_layout"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

  <fragment
      android:name="com.bencallis.dealpad.DealListFragment"
      android:id="@+id/deal_list_fragment"
      android:layout_weight="1"
      android:layout_width="0px"
      android:layout_height="match_parent" >
      <!-- Preview: layout=@layout/deal_list_fragment -->
  </fragment>
  <View
      android:id="@+id/my_container"
      android:layout_weight="2"
      android:layout_width="0px"
      android:layout_height="match_parent" >
  </View>

</LinearLayout>

and then in Java, your onCreate method:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.my_container, new DealDetailsFragment());
transaction.commit();

or even better create whole method to just deal with Transactions.

Now Transaction from your question should work. :)

Radtke answered 6/2, 2012 at 14:20 Comment(2)
Thanks for the help. I have changed my layout replacing the fragment with a view. In my main activity I added the following FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.right_fragment_container, new DealDetailsFragment(), "dealDetailsFragment"); transaction.commit(); Unfortunately this is resulting in a Java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroupVadose
I have sorted this cast exception by using LinearLayout rather than View. The application now runs but when pressing go to deals a url can not be loaded as webView is still null.Vadose

© 2022 - 2024 — McMap. All rights reserved.