Android:在 Activity 生命周期中何时调用 onCreateOptionsMenu? [英] Android: When is onCreateOptionsMenu called during Activity lifecycle?

查看:43
本文介绍了Android:在 Activity 生命周期中何时调用 onCreateOptionsMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 onCreate 中放置了几个断点(一个在方法的开头,一个在方法的结尾),我也在 onCreateOptionsMenu.onCreate 方法首先被调用,在它完成之前 onCreateOptionsMenu 被调用.

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.

我试图在我的应用程序中分离 Fragment 导航代码,所以我有几个对象,我将 onCreateOptionsMenu 委托给它,具体取决于应用程序是否正在运行在手机/平板电脑上(我使用屏幕尺寸来确定这一点,我的大屏幕布局文件有一个视图,我在布局膨胀后检查).我遇到的问题是,我在 onCreate 中创建了这些对象,当我在 onCreateOptionsMenu 中引用该对象时,我得到了一个空指针异常.

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.

推荐答案

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

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

在带有官方 Honeycomb 样式操作栏的设备和应用程序上也是如此.如果没有操作栏,onCreateOptionsMenu() 不应该被调用,直到用户调用菜单,通常是按下 MENU 按钮.

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)

一旦冰淇淋三明治发货,该测试很快就会中断.据我所知,ICS 手机会有操作栏(虽然可能没有系统栏).

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

这篇关于Android:在 Activity 生命周期中何时调用 onCreateOptionsMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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