Java 使用权重pamameter动态创建片段

Java 使用权重pamameter动态创建片段,java,android,dynamic,android-fragments,Java,Android,Dynamic,Android Fragments,我正在创建一个应用程序,我必须在其中动态添加3个片段,每个片段应该占用相同的空间。为此,我使用了LayoutParams,但它不起作用,它给出了Java.lang.NullPointerException。 请帮我解决这个错误 片段类 public class HeaderFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,

我正在创建一个应用程序,我必须在其中动态添加3个片段,每个片段应该占用相同的空间。为此,我使用了LayoutParams,但它不起作用,它给出了Java.lang.NullPointerException。 请帮我解决这个错误

片段类

public class HeaderFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.timetable_activity, container, false);
}
}
主要活动

public class TimeTableActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.timetable_activity);
    super.onCreate(savedInstanceState);
    if (findViewById(R.id.fragment_layout) != null) {

        if (savedInstanceState != null) {
            return;
        }

        // Create an instance of ExampleFragment
        HeaderFragment firstFragment = new HeaderFragment();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);

        firstFragment.getView().setLayoutParams(params);
        BodyFragment secondFragment = new BodyFragment();
        secondFragment.getView().setLayoutParams(params);
        FooterFragment thirdFragment = new FooterFragment();
        thirdFragment.getView().setLayoutParams(params);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_layout, firstFragment).commit();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_layout, secondFragment).commit();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_layout, thirdFragment).commit();

    }

}
}
Main Activty.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

</LinearLayout>
试试这个:

XML:

在活动的onCreate中,您正在创建一个新片段,并立即尝试访问其视图:

    HeaderFragment firstFragment = new HeaderFragment();
    ...
    firstFragment.getView().setLayoutParams(params);
这将不起作用,因为当片段连接到活动时,布局将膨胀,您将在下面执行几行操作:

    getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_layout, firstFragment).commit();
请记住,将片段附加到活动是异步的,因此在onCreate中简单地重新排列语句并不能解决问题。如果只需要修改片段的视图,则应在片段的onViewCreated中设置布局参数;如果需要访问父活动的视图层次结构,则应在onAttach中设置布局参数

我建议您通过在布局中实例化片段来避免这种情况。例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    tools:context=".MainActivity" >

    <fragment android:name="fully.qualified.path.to.HeaderFragment"
              android:id="@+id/first_fragment"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="0dp" />

    <fragment android:name="fully.qualified.path.to.HeaderFragment"
              android:id="@+id/second_fragment"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="0dp" />

    <fragment android:name="fully.qualified.path.to.HeaderFragment"
              android:id="@+id/third_fragment"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="0dp" />

</LinearLayout>

id为R.id.fragment\u布局的视图在哪里?。第27行TimeTableActivity是什么?但进一步说,我应该能够删除片段并动态添加另一个片段。如果我像您所说的那样用xml声明它,这是可能的吗?Yes,您关于FragmentManager的逻辑看起来不错。我添加了在onViewCreated of fragment中设置参数的代码。现在它工作正常:非常感谢:
    HeaderFragment firstFragment = new HeaderFragment();
    ...
    firstFragment.getView().setLayoutParams(params);
    getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_layout, firstFragment).commit();
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    tools:context=".MainActivity" >

    <fragment android:name="fully.qualified.path.to.HeaderFragment"
              android:id="@+id/first_fragment"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="0dp" />

    <fragment android:name="fully.qualified.path.to.HeaderFragment"
              android:id="@+id/second_fragment"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="0dp" />

    <fragment android:name="fully.qualified.path.to.HeaderFragment"
              android:id="@+id/third_fragment"
              android:layout_weight="1"
              android:layout_width="match_parent"
              android:layout_height="0dp" />

</LinearLayout>