安卓:当onCreateOptionsMenu在活动周期叫什么名字? [英] Android: When is onCreateOptionsMenu called during Activity lifecycle?

查看:174
本文介绍了安卓:当onCreateOptionsMenu在活动周期叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把一对夫妇的断点在的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.

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).

这篇关于安卓:当onCreateOptionsMenu在活动周期叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆