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).
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
?
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
Fragment
s view is loaded, hence yourNullPointerException
. 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:
to load your Fragment, use:
This class also includes a convenience method for loading a url, that won't throw an
Exception
if there's noWebView
.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!
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:
and then in Java, your onCreate method:
or even better create whole method to just deal with
Transaction
s.Now
Transaction
from your question should work. :)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.
Hopefully this is useful to someone in the future.
WebViewFragment as is is not that straightforward to use. Try this simple extension (You can copy/paste):
Call where you need using the factory method: