[英]how to implement fragment in android
am currently messing around with DialogFragment to learn to use it. 目前正在与DialogFragment混在一起以学习使用它。 I assumed that compared to
onCreateView()
, onCreate()
can do this: 我假设与
onCreateView()
相比, onCreate()
可以做到这一点:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testTextView.setText("SUCCESS!"); //ERROR HERE
}
But I am wrong. 但是我错了。 Not sure why its not working.
不知道为什么它不起作用。 The error goes away when I comment out
testTextView.setText("Success!");
当我注释掉
testTextView.setText("Success!");
时,错误消失了testTextView.setText("Success!");
The error is a NullPointerException
, and then it just flags line 39 which is where the offending line of code is. 错误是
NullPointerException
,然后仅标记了行39,这是有问题的代码所在的位置。 Any clarifications much appreciated. 任何澄清非常感谢。
Try this : 尝试这个 :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (container == null) {
return null;
}
return (LinearLayout)inflater.inflate(R.layout.your_fragmentlayout_file, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstance)
{
super.onActivityCreated(savedInstance);
TextView testTextView = (TextView)getActivity().findViewById(R.id.your_textview_id);
testTextView.setText("SUCCESS!");
}
where you initalzed testTextView. 您在其中安装了testTextView的位置。 ?
? you should do that.........
你应该那样做.........
and also as per fragment life cycle 以及每个片段的生命周期
onAttach(Activity)-->onCreate(Bundle) -->onCreateView(LayoutInflater, ViewGroup, Bundle)->onActivityCreated(Bundle)-->onStart()
so you should handle this work either in onCreateView or after this... 所以您应该在onCreateView或之后处理此工作...
http://android-developers.blogspot.in/2012/05/using-dialogfragments.html http://android-developers.blogspot.in/2012/05/using-dialogfragments.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.