[英]Getting NullPointerException while sending data from activity to fragment
I have implemented the fragment in android. 我已经在android中实现了该片段。 Now we try to send the data from activity to the fragment.
现在,我们尝试将数据从活动发送到片段。 But while sending the data we are getting the NullPointerException in
*onCreateView*
method of fragment. 但是,在发送数据时,我们在片段的
*onCreateView*
方法中获取了NullPointerException 。
Following code of 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();
Following code of 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);
}
what is the mistake i did, because of that i am getting NullPointerException , please help 我犯了什么错误,因此我得到了NullPointerException ,请帮助
Try below code:- 试试下面的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
String strtext = this.getArguments().getString("sessionName");
TextView sessionTitle = (TextView) view.findViewById(R.id.session1);
sessionTitle.setText(strtext);
return view;
}
your not getting view and try to set value on textview. 您没有获得视图,并尝试在textview上设置值。
You have 你有
TextView sessionTitle = (TextView) getView()
.findViewById(R.id.session1);
getView()
returns null in your case. 在您的情况下,
getView()
返回null。 So sessionTitle
is null and when you call setText()
on null you get NUllPointerException
. 所以
sessionTitle
为null,当您对null调用setText()
,会得到NUllPointerException
。
SO change to 所以变成
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = (VIew)inflater.inflate(R.layout.fragment_1, container, false);
// inflate tha layout
String strtext = this.getArguments().getString("sessionName");
// get the string
TextView sessionTitle = (TextView) view
.findViewById(R.id.session1);
// initialize textview or initialize it in onActivityCreated using getView
sessionTitle.setText(strtext);
// set text to textview
return view
}
You can use getView().findViewById(id)
in onActivityCreated()
您可以在
onActivityCreated()
使用getView().findViewById(id)
onActivityCreated()
while getting the id of textview you are getting nullpointer exception ,because of using getview(),getview is used when we get the id in onActivityCreated method(). 在获取textview的ID时,您会得到nullpointer异常 ,因为使用getview(),所以在onActivityCreated method()中获取ID时会使用getview。 just change getview() to view will remove this exception.just chnage :
只需更改getview()以查看将删除此异常。只需chnage:
TextView sessionTitle = (TextView)view.findViewById(R.id.session1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.