WebViewFragment webView is null after doing a FragmentTransaction

Сейчас у меня в приложении настроен ListFragment слева и DetailsFragment справа (аналогично схеме на планшете ниже).

layout

На фрагменте деталей (фрагмент рядом со списком) у меня есть кнопка goto deal, которая при нажатии должна заменить фрагмент DetailsFragment на WebViewFragment.

Проблема в том, что при попытке загрузить url во фрагмент webview WebView - 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");

Ниже приведен мой основной макет, в котором определены два исходных фрагмента.

<?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>

Похоже, что фрагмент webViewFragment создается не полностью, поскольку WebView не был инициализирован. Я поискал в Интернете, но информации о WebViewFragment очень мало.

Есть идеи, как убедиться, что WebView инициализируется в WebViewFragment?

13
задан Community 8 February 2017 в 14:34
поделиться