[英]When does android's ListActivity class call setContentView()?
I'm trying to use the requestWindowFeature()
function to set a custom title view on a list activity. 我正在尝试使用requestWindowFeature()
函数在列表活动上设置自定义标题视图。 The method works fine with a view that only subclasses Activity
, but whenever I try the same method with a ListActivity
subclass, it errors, giving me a NullPointerException when I try to programmatically modify the title view. 该方法在仅将Activity
子类化的视图中工作良好,但是每当我尝试使用ListActivity
子类使用同一方法时,它都会出错,当我尝试以编程方式修改标题视图时,出现NullPointerException。
I believe the problem pertains to the fact that requestWindowFeature()
needs to be called before setContentView()
. 我相信问题与在setContentView()
之前需要调用requestWindowFeature()
setContentView()
。 Because ListActivity
takes care of setting the content view for you, I don't know when that is being called. 因为ListActivity
会为您设置内容视图,所以我不知道何时调用它。 Does anyone have any suggestions? 有没有人有什么建议? Thanks for the help. 谢谢您的帮助。
Sometimes, requestWindowFeature()
gives a NullPointerException if it's called after the super.onCreate(bundle);
有时,如果在super.onCreate(bundle);
之后调用requestWindowFeature()
, requestWindowFeature()
提供NullPointerException super.onCreate(bundle);
call. 呼叫。 Another reason is if you have called setContentView
first as well. 另一个原因是,如果您也首先调用过setContentView
。
super.onCreate(bundle);
and setContentView
must be called after all requestWindowFeature()
calls are made. 和setContentView
必须在进行所有requestWindowFeature()
调用之后调用。
ListActivity
doesn't take care of calling
setContentView
for you: you still need to do it yourself.
ListActivity
不会为您调用
setContentView
:您仍然需要自己进行操作。
Your content view has to have an appropriately-named
ListView
but you still need to call
setContentView
yourself.
您的内容视图必须具有一个适当命名的
ListView
但是您仍然需要自己调用
setContentView
。
Just call
requestWindowFeature
right after the
super.onCreate
call in
onCreate
and then call
setContentView
after that and you should be golden.
只需拨打
requestWindowFeature
后右
super.onCreate
呼叫
onCreate
,然后调用
setContentView
之后,你应该是金色的。
EDIT: my mistake, you are quite right (I didn't know that: I've always just called setContentView
with a custom layout). 编辑:我的错误,您说得很对(我不知道:我一直只是使用自定义布局调用setContentView
)。
It appears from here that there is no way to slip a requestWindowFeature
call before the setContentView
call in ListActivity
: it ALWAYS calls setContentView
immediately after the super.onCreate
call. 从这里看来,没有办法在ListActivity
的setContentView
调用之前滑动requestWindowFeature
调用:它总是在super.onCreate
调用之后立即调用setContentView
。 You can try calling requestWindowFeature
before you call super.onCreate
but I suspect that won't work any better. 你可以尝试调用requestWindowFeature
打电话之前super.onCreate
但我怀疑这是行不通的任何好转。
I don't think you will be able to use the default ListActivity
for this: you'll probably need to use a regular Activity and manually do the ListView bindings. 我认为您不能为此使用默认的ListActivity
:您可能需要使用常规的Activity并手动执行ListView绑定。
The solution to my problem, for any googlers, was to copy the source of the ListActivity
class, and also the layout_content.xml file into my own app's package, and subclass from this instead of Android's ListActivity
class. 对于所有Google员工来说,解决我的问题的方法是将ListActivity
类的源代码以及layout_content.xml文件复制到我自己的应用程序的程序包中,并从中子类化,而不是Android的ListActivity
类。 I then added an onCreate()
method to this class where I set request the window feature(s) and then call the setContentView()
method. 然后,我向此类添加了onCreate()
方法,在该类中我设置了请求窗口功能的要求, 然后调用setContentView()
方法。 Hack? 哈克? Probably. 大概。 Works? 作品? Yes :) 是的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.