Java 以编程方式将布局添加到片段

Java 以编程方式将布局添加到片段,java,android,layout,android-fragments,Java,Android,Layout,Android Fragments,导言 根据用户从导航抽屉中的选择,我希望有不同的片段。对于导航抽屉中的每个项目,都应该调用不同的站点。我想通过碎片来实现这一点。 通过在onCreate方法中执行以下操作,将首先显示主片段 if ( savedInstanceState == null ) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment fragment = MainFragment.passL

导言 根据用户从导航抽屉中的选择,我希望有不同的片段。对于导航抽屉中的每个项目,都应该调用不同的站点。我想通过碎片来实现这一点。 通过在onCreate方法中执行以下操作,将首先显示主片段

if ( savedInstanceState == null ) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment fragment = MainFragment.passList(hashMap);
    ft.replace(R.id.content_frame, new MainFragment());
    ft.commit();
}
正如您所看到的,我正在将数据(一个hashmap)传递给片段的一个方法,该方法工作得非常好。然后将数据放入字符串数组中。这是可行的,数据也可以用在片段的onCreate方法中

这是主要活动的xml

    <android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/drawer_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >

 <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:choiceMode="singleChoice"
       android:divider="@android:color/darker_gray"
       android:dividerHeight="0.1dp" />

</android.support.v4.widget.DrawerLayout>
但这只会给我留下一个空白屏幕,没有实际添加的文本。我怎样才能做到这一点?数据就在那里-我知道,因为它是用System.out.println打印出来的-我只需要一种方式来显示它。提前谢谢

编辑

当我这样做的时候

View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
我得到的只是一个非法状态的例外,ScrollView只能托管一个直接子对象。。。但我不明白。线性布局是唯一直接的孩子。为什么会有问题

下面是完整的
onCreateView
-方法

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
    source      = new TextView[hashMapSize];


    for (int i = 0; i < hashMapSize; i++) {

        source[i]       = new TextView(getActivity());
source      [i].setText( "Source: "     + sourceArr     [i] );
ll.addView(source[i]

    }
    return rootView;
}
public View onCreateView(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.main_碎片,容器,假);
ScrollView sv=(ScrollView)rootView.findViewById(R.id.mainScrollView);
LinearLayout ll=(LinearLayout)rootView.findviewbyd(R.id.mainListView);
sv.addView(ll);
source=新文本视图[hashMapSize];
for(int i=0;i
您获得
非法状态异常的原因是因为您膨胀了R.layout.main\u片段,该片段已经包含已经有一个直接子级(在布局xml中声明)的ScrollView

您不应该使用
sv.addView(ll)
添加代码。您需要做的就是像使用
findViewById
一样获取引用


去掉sv.addView(ll);
你应该会没事的。

有时候只要一双新眼睛就够了:)
View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
    source      = new TextView[hashMapSize];


    for (int i = 0; i < hashMapSize; i++) {

        source[i]       = new TextView(getActivity());
source      [i].setText( "Source: "     + sourceArr     [i] );
ll.addView(source[i]

    }
    return rootView;
}