In a DialogFragment, what should onCreate do?

老子叫甜甜 提交于 2019-12-04 19:09:55

testTextView is NOT pointing to any object so try something like

testTextView = (TextView) findViewById(R.id.testTextView);

EDIT:

If you see the lifecycle of a fragment, it says that onCreateView is called after onCreate hence your onCreate doesn't have any reference to your object, that is textview in your layout

You haven't used setContentView yet, so you are getting a NPE for the TextView.

onCreate happens before onCreateView. If you want to access something from the layout there, you need to setContentView... which is not a good idea for a DialogFragment.

Move that bit of code to onCreateView after you setContentView and you'll be ok.

For your reference, here's the Fragment Lifecycle:

Did you initialize the testTextView in onCreateView? You have to use a LayoutInflater in onCreateView to get the Layout and then you have to access the TextView via findViewById.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!