2014-10-10 132 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