简体   繁体   English

android的ListActivity类何时调用setContentView()?

[英]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. 谢谢您的帮助。

setContentView is called whenever you interact with the List, for example calling getList() or setAdapter() on listactivity. 每当您与List交互时,都会调用setContentView,例如,在listactivity上调用getList()或setAdapter()。 See the source of listactivity 查看listactivity的来源

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. 这里看来,没有办法在ListActivitysetContentView调用之前滑动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.

相关问题 Android Java | 当我调用setContentView()时,InflateException错误使类膨胀 - Android Java | InflateException error inflating class when I call setContentView() setContentView如何在Android中工作? - How does setContentView work in android? Android:可以在setContentView之后调用requestFeature()吗? - Android : is it possible to call requestFeature() after setContentView ? android:自己的类扩展活动和/或listactivity - android: own class extending activity and/or listactivity Android:ListActivity不显示任何项目 - Android: ListActivity does not display any Item 从“ ListActivity extended”中调用“ Activity扩展”类的方法 - Call a method of “Activity extended” class from “ListActivity extended” 在java gradle android项目中的setContentView调用上获取OOM问题 - Getting an OOM issue on the setContentView call in a java gradle android project Android:从活动到活动,第二个活动无法调用setContentView? - Android: Activity to Activity, 2nd activity cannot call setContentView? 如何在扩展View的类中设置ContentView? (Android,Java) - How do I setContentView in a class that extends View? (Android, Java) 来自 Android 中不同 Class 的 setContentView(R.layout.xy) - setContentView(R.layout.xy) from different Class in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM