我目前正在摆弄 DialogFragment 以学习使用它。我认为与 onCreateView()
相比,onCreate()
可以做到这一点:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testTextView.setText("SUCCESS!"); //ERROR HERE
}
但是我错了。不知道为什么它不起作用。当我注释掉 testTextView.setText("Success!");
时,错误消失了
该错误是 NullPointerException,然后它仅标记第 39 行,这是有问题的代码行所在的位置。非常感谢任何澄清。
编辑:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View testView = (View)inflater.inflate(R.layout.test_dialog_file, container, false);
//instantiated testTextView globally
testTextView = (TextView)testView.findViewById(R.id.set_text);
testTextView.setText("SUCCESS!");
return testView;
}
最佳答案
testTextView 未指向任何对象,因此请尝试类似的内容
testTextView = (TextView) findViewById(R.id.testTextView);
编辑:
如果您看到 lifecycle of a fragment ,它表示 onCreateView
是在 onCreate
之后调用的,因此您的 onCreate
没有对您的对象的任何引用,这是布局中的 TextView
关于android - 在DialogFragment中,onCreate应该做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11046075/