android开发中一个暴简单的问题,为什么不能在Activity中的属性里创建Button的实例,会报空指针
展开全部
资源还没创建好就调用,所以是空指针。
关于super.onCreate()的注释:
Called when the activity is starting. This is where most initialization should go: calling setContentView(int)} to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI etc.
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
关于findViewById(int id)的注释:
Finds a view that was identified by the id attribute from the XML that was processed in onCreate,return The view if found or null otherwise.
关于super.onCreate()的注释:
Called when the activity is starting. This is where most initialization should go: calling setContentView(int)} to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI etc.
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
关于findViewById(int id)的注释:
Finds a view that was identified by the id attribute from the XML that was processed in onCreate,return The view if found or null otherwise.
追问
哦我懂了,我本来以为系统会先new一个Activity,然后在再调用oncreate。那这里系统是怎么创建Activity实例呢?是直接用oncrete?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你得把初始化放在oncreate方法里面,因为这个方法会执行控件绘制等一系列操作,可以查看一下源码。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询