我希望在滑动标签片段上有一个信息片段,并且能够在创建信息片段时将一些参数传递给信息片段。我已经(希望所有)提供以下相关代码:
InfoFragment.java:
public class InfoFragment extends Fragment
{
private static final String HEADER_ARG = "HEADER";
private String header;
/**
* Use this factory method to create a new instance of this fragment using the provided parameters.
*/
public static InfoFragment newInstance(String header)
{
InfoFragment fragment = new InfoFragment ();
Bundle args = new Bundle();
args.putString(HEADER_ARG, header);
fragment.setArguments(args);
return fragment;
}
/**
* Required empty public constructor
*/
public InfoFragment() {}
/**
* Gets the args out of the bundle
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
this.header = getArguments().getString(HEADER_ARG);
}
else
{
Log.e("Frag", "Args = null");
}
}
// Rest of Fragment...
}
TabFragment.java:
public class TabFragment extends Fragment
{
/**
* Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
*/
public TabFragment() {}
/**
* Create the Inventories tab view
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_2, container, false);
return view;
}
/**
* Dynamically add fragments
*/
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
InfoFragment infoFrag = InfoFragment.newInstance("MyHeader");
FragmentManager fm = getChildFragmentManager();
fm.beginTransaction().add(R.id.info_fragment, infoFrag);
}
// Rest of TabFragment...
}
tab_fragment.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/character_item_fragment_1"
android:name="lomax.mycharacter3.InfoFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
控制台日志显示getArgs()= null,我不明白为什么。有什么想法吗?
答案 0 :(得分:1)
因为您正在使用片段标记,您的片段将使用空构造函数进行实例化,您也忘记在事务结束时调用commit,这就是为什么您没有获得有关视图的异常缺少身份证。
修正版: tab_fragment.xml:
generate_pdf
代码:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/info_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>