我在创建片段的代码中存在以下问题,这没关系。
我的问题是当我尝试设置他和参数。
这是我的代码。以编程方式创建片段并设置参数
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentImageList myFragment = new FragmentImageList();
fragmentTransaction.add(631, myFragment, "fragmentTag");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
myFragment.getView().setLayoutParams(params);
然后在控制台中我有以下错误。
Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
由于Fragment的视图还没有被创建,其中有意义。 但如何做到这一点来设置创建片段的“参数”。
因为在此之前,我在代码中创建了LinearLayout,这是他的ID。 目标是创建一个没有GridLayout的行和列的网格。 – OneByte