我目前已經在左側設置了ListFragment
,在右側設置了DetailsFragment
(類似於下面的平板電腦上的佈局)。做一個FragmentTransaction後,WebViewFragment的webView爲空
在細節片段(列表旁邊的片段)我有一個轉到的交易按鈕,按下該按鈕應以WebViewFragment
更換detailsFragment時。
我遇到的問題是,當試圖在webviewfragment中加載url時,WebView
爲空。
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: [email protected]/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: [email protected]/deal_details_fragment -->
</fragment>
</LinearLayout>
似乎沒有完全被創建webViewFragment作爲WebView
尚未初始化。我在網上查找過,但關於WebViewFragment
的信息很少。
任何想法如何確保WebView
初始化在WebViewFragment
?
請張貼代碼爲您DealWebViewFragment類。 – Jonathan 2012-02-06 14:12:52
@Jonathan - 對不起,我的DealWebViewFragment只是重新創建了WebViewFragment。我已將上面的代碼更改回WebViewFragment(存在同樣的問題)。 – bencallis 2012-02-06 18:16:04