安卓:当onCreateOptionsMenu在活动周期叫什么名字?叫什么名字、周期、onCreateOptionsMenu

2023-09-12 04:13:33 作者:一见你就笑

我把一对夫妇的断点在的onCreate (一个开头,一个在方法结束),我也把一只之初 onCreateOptionsMenu 。该的onCreate 方法被调用首先,它完成之前 onCreateOptionsMenu 被调用。

我想分开片段导航code。在我的应用程序,所以我有一对夫妇,我委托的对象 onCreateOptionsMenu 来依靠,如果应用程序是在手机/平板电脑上运行(我使用的屏幕尺寸来确定这一点,我对大屏幕的布局文件中有一个视图我检查的布局是在充气后)。我遇到的问题是,我的onCreate创建这些对象,并且我得到一个空指针异常,当我引用 onCreateOptionsMenu 的对象。

解决方案   

在onCreate方法首先被调用,它完成onCreateOptionsMenu被调用之前。

这将是对设备和应用程序与正式的蜂窝式操作栏真。如果没有行动起来吧, onCreateOptionsMenu()不应该被调用,直到用户调出菜单,通常由pressing MENU(菜单)按钮。

  

(我使用的屏幕尺寸来确定这一点,我对大屏幕的布局文件中有一个视图我检查的布局是在充气后)

该测试将打破很快,一旦冰淇淋三明治的船只。从我可以告诉,ICS手机将有动作条(虽然也许不是系统吧)。

I put a couple of breakpoints in onCreate (one at the beginning, and one at the end of the method), and I also put one at the beginning of onCreateOptionsMenu. The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.

高校第二课堂大数据周报 4月29日 5月5日

I'm trying to separate the Fragment navigation code in my app, so I have a couple of objects that I delegate onCreateOptionsMenu to depending on if the app is running on phone/tablet (I'm using screen size to determine this, my layout file for large screens has a View I check for after the layout is inflated). The problem I'm having is, I create these objects in onCreate, and I'm getting a null pointer exception when I reference the object in onCreateOptionsMenu.

解决方案

The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.

That will be true on devices and apps with an official Honeycomb-style action bar. If there is no action bar, onCreateOptionsMenu() should not get called until the user calls up the menu, typically by pressing the MENU button.

(I'm using screen size to determine this, my layout file for large screens has a View I check for after the layout is inflated)

That test will break very shortly, once Ice Cream Sandwich ships. From what I can tell, ICS phones will have action bars (though perhaps not system bars).