onCreateOptionsMenu()未在FragmentActivity上调用 [英] onCreateOptionsMenu() is not getting called on FragmentActivity

查看:139
本文介绍了onCreateOptionsMenu()未在FragmentActivity上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同事,MainActivity类是从FragmentActivity派生的,由于某种原因,未调用活动的onCreateOptionsMenu().我在onCreate()中有第一个断点,它被触发,在onCreateOptionsMenu()中有第二个断点,它没有被触发.

Colleagues, MainActivity class is derived from FragmentActivity, and for some reason activity's onCreateOptionsMenu() is not getting called. I have the first breakpoint in onCreate(), which is getting triggered, and the second one in onCreateOptionsMenu(), which is not getting triggered.

import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    private FragmentPagerAdapter m_fragmentPagerAdapter;
    private ViewPager m_viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the  primary sections of the activity.
        m_fragmentPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        m_viewPager = (ViewPager) findViewById(R.id.container);
        m_viewPager.setAdapter(m_fragmentPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);      // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }
}

此应用程序中的片段没有自己的菜单.属于该活动的一个菜单覆盖"了所有内容.

Fragments in this app don't have their own menus. One menu belonging to the activity "covers" everything.

主题是Holo.Light

Theme is Holo.Light

什么阻止了onCreateOptionsMenu()的呼叫?我想念什么?

What prevent onCreateOptionsMenu() from getting called? What am I missing?

推荐答案

仔细检查您是否已将应用程序主题真正设置为Holo.Light.我无法解释导致问题的原因,但是我能够重现该问题.在尝试了FragmentActivityAppCompatActivity之间的更改之后,我意外地使用了FragmentActivity并将主题设置为Theme.AppCompat.Light.这样就创建了您看到的行为,未调用onCreateOptionsMenu().

Double-check that you have really set the app theme to Holo.Light. I can't explain what is causing the problem, but I was able to reproduce it. After experimenting with changes between FragmentActivity and AppCompatActivity, I accidentally ran with FragmentActivity and theme set to Theme.AppCompat.Light. This created the behavior you are seeing, with onCreateOptionsMenu() not called.

作为附带问题,您应按照

As a side issue, you should fix your onCreateOptionsMenu() to call through to super as noted in the documentation: "Deriving classes should always call through to the base implementation".

这篇关于onCreateOptionsMenu()未在FragmentActivity上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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