开发者

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. 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 开发者_JS百科when I try to programmatically modify the title view.

I believe the problem pertains to the fact that requestWindowFeature() needs to be called before setContentView(). Because ListActivity takes care of setting the content view for you, I don't know when that is being called. Does anyone have any suggestions? Thanks for the help.


setContentView is called whenever you interact with the List, for example calling getList() or setAdapter() on listactivity. See the source of listactivity


ListActivity doesn't take care of calling setContentView for you: you still need to do it yourself. Your content view has to have an appropriately-named ListView but you still need to call setContentView yourself. Just call requestWindowFeature right after the super.onCreate call in onCreate and then call setContentView after that and you should be golden.

EDIT: my mistake, you are quite right (I didn't know that: I've always just called setContentView with a custom layout).

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. You can try calling requestWindowFeature before you call super.onCreate but I suspect that won't work any better.

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.


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. I then added an onCreate() method to this class where I set request the window feature(s) and then call the setContentView() method. Hack? Probably. Works? Yes :)


Sometimes, requestWindowFeature() gives a NullPointerException if it's called after the super.onCreate(bundle); call. Another reason is if you have called setContentView first as well.

super.onCreate(bundle); and setContentView must be called after all requestWindowFeature() calls are made.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜