2014-10-10 136 views
-1

我想要得到一個片段以顯示在Android中。當我執行代碼去正確的片段時,我所看到的就是敬酒。 fragment_detail沒有出現。有沒有人知道這段代碼有什麼問題? 從調用類:Android片段不能正常工作

  final FragmentManager fm = getActivity().getSupportFragmentManager(); 
      final FragmentTransaction ft = fm.beginTransaction(); 
      DetailFragment df = new DetailFragment(); 
      ft.replace(R.id.pager, df, "DetailFragment").setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit(); 

從所謂類:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_detail, container, false); 
     Toast.makeText(getActivity().getBaseContext(), "Blah de Blah", Toast.LENGTH_LONG).show(); 
     return rootView; 
    } 

的佈局fragment_detail:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<FrameLayout 
android:id="@+id/fragment_content" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Large Text" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 
</FrameLayout> 

</LinearLayout> 

謝謝!

+0

(R.id.pager,DDF,「 DetailFragment「)'ddf或df? – 2014-10-10 04:58:48

+0

對不起,錯字已被更正。 – user2078519 2014-10-10 16:30:31

回答

1

它看起來像它可能是一個XML的問題,請嘗試更改如下:

<FrameLayout 
android:id="@+id/fragment_content" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> /> 

應該是:

<FrameLayout 
android:id="@+id/fragment_content" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</FrameLayout> 
在`ft.replace
+0

謝謝,我糾正了這個問題,但是,我仍然有同樣的問題。有任何想法嗎? – user2078519 2014-10-10 18:59:27