2013-03-01 191 views
0

我正在試驗Android碎片,因此我創建了兩個碎片ListFragmentDetailFragment。問題是,當我點擊ListFragment並調用DetailFragment方法顯示ListFragment中的選定項目時,DetailFragment上未顯示任何結果。這裏是DetailFragment代碼:Android碎片

private static final String DETAIL_FRAG_TAG = "detail_fragment"; 
private Context appContext = null; 
private TextView lblItemDetail = null; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // inflate the fragment layout 
    View rootView = inflater.inflate(R.layout.fragments_detail_fragment, container, false); 
    lblItemDetail = (TextView) rootView.findViewById(R.id.lbl_itemDetail); 


//at this point the TextView is not null===>see L0g.i 
Log.i(DETAIL_FRAG_TAG, " ---MyDetailFragment---oncreateView()--lblItemDetail =[" + lblItemDetail + "]"); 

    // get the fragment activity context 
    appContext = this.getActivity(); 
    return rootView; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 

} 

/** 
* show the details of the item selected on the listFragment. 
* @param itemDetail - the details of the item selected on ListFragment. 
*/ 
public void showLstItemDetail(String itemDetail) { 

    if (lblItemDetail != null) { 
     // the View to show Text should not be Null. 
     lblItemDetail.setText(itemDetail); 
    } 

    //at this point calling this method shows 
      that the `TextView` is Null yet it's 
     initialized in the 
     oncreate() as a class member variable ---why am i 
    getting Null after the `oncreate` is finished. 
    Log.i(DETAIL_FRAG_TAG, "------showItemDetail---------msg=[" + itemDetail + "] txt=[" + lblItemDetail + "]"); 
} 



//when I create an instance of `MYDetailFragment` and call the method to show the details of item Selected on the `DetailFragment` the `TextView` will be null. Why? 

    MYDetailFragment detailFrag = new MyDetailFragment(); 
    detailFrag.showLstItemDetail("Selected List Item"); 
+0

你能告訴我們如何以及何時你叫'MYDetailFragment detailFrag'?你可以c/p你所有的片段代碼? – 2013-03-01 14:52:04

+0

您應該使用setArguments和getArguments將詳細信息傳遞給片段。您正在調用detailFrag.showLstItemDetail(「Selected List Item」)的Currenlty; - >這個函數應該在片段本身中調用。爲什麼?因爲那時片段ui尚未加載 – Tobrun 2013-03-01 15:11:28

回答

0
這兩條線在

片段下面的教程:

MYDetailFragment detailFrag = new MyDetailFragment(); 
detailFrag.showLstItemDetail("Selected List Item"); 

onCreateView()當時尚未被稱作。這意味着片段rootView永遠不會被創建,並且TextView永遠不會被創建!

只有在您使用片段事務後才能創建視圖,將該片段放入佈局中,然後片段將被附加到活動(onAttach()),並且在onCreateView()調用。只有這樣才能設置任何東西。

將參數傳遞給片段的標準最佳做法是使用Bundle。看一個例子代碼:

上的活動:

MYDetailFragment detailFrag = new MYDetailFragment(); 
Bundle b = new Bundle(); 
detailFrag.setArguments(b); 
b.putString("detail", value); 
// then proceed to the fragment transaction 

然後在您的片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
// inflate the fragment layout 
View rootView = inflater.inflate(R.layout.fragments_detail_fragment, container, false); 
lblItemDetail = (TextView) rootView.findViewById(R.id.lbl_itemDetail); 
Bundle b = getArguments(); 
lblItemDetail.setText(b.getString("details"));