【发布时间】:2014-10-08 05:49:05
【问题描述】:
我已经在 android 中实现了片段。
现在我们尝试将数据从活动发送到片段。但是在发送数据时,我们在片段的*onCreateView* 方法中得到了 NullPointerException。
以下Activity的代码。
setContentView(R.layout.fragment_1);
FragmentManager fm = getFragmentManager();
FragmentTranstction ft = fm.beginTransaction();
Fragment1 f1 = new Fragment1();
String text1 = "prakash";
Bundle bundle = new Bundle();
bundle.putString("sessionName", text1);
f1.setArguments(bundle);
ft.add(R.id.frag1, f1);
ft.commit();
Fragment 代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = this.getArguments().getString("sessionName");
TextView sessionTitle = (TextView) getView()
.findViewById(R.id.session1);
sessionTitle.setText(strtext);
return inflater.inflate(R.layout.fragment_1, container, false);
}
我做错了什么,因为我得到NullPointerException,请帮助
【问题讨论】:
-
将字符串获取代码更改为 onActivityCreate() 覆盖方法。它在 oncreateview 之后出现
-
Activity 和 Fragment 之间的通信看这个:stackoverflow.com/questions/24321449/…
标签: java android android-fragments