动作条的菜单项上的一些安卓4.1.2设备不可见 [英] ActionBar menu items not visible on some Android 4.1.2 devices

查看:90
本文介绍了动作条的菜单项上的一些安卓4.1.2设备不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两位一个月前我发布了一个Android应用程序V14为的minSdkVersion和V19作为TargetSDKVersion。一切工作正常,旁边的一些用户报告的问题与动作条菜单项:

two month ago I released an Android app v14 as MinSDKVersion and v19 as TargetSDKVersion. Everything works fine, beside that some users reports problem with the ActionBar menu items:

它们显示在第一个应用程序的开始,但在第一次切换远离标签消失。

They are shown on first app start but vanish when first switching away from a tab.

在App的MainActivity延伸 FragmentActivity ,并使用动作条的标签,以3个不同的页面(片段)之间切换:

The MainActivity of the App extends FragmentActivity and uses ActionBar Tabs to switch between 3 different pages (Fragments):

public class MainActivity extends FragmentActivity {
    protected void onCreate(Bundle savedInstanceState) {
        ...
        sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        viewPager = (ViewPager)findViewById(R.id.pager);
        viewPager.setAdapter(sectionsPagerAdapter);

        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                getActionBar().setSelectedNavigationItem(position); 
            }
        });

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            public void onTabSelected(Tab tab, android.app.FragmentTransaction arg1) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) { }
            public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) { }
        };

        actionBar.addTab(actionBar.newTab()
                .setText(R.string.LOC_Common_Overview)
                .setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab()
                .setText(R.string.LOC_Contacts)
                .setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab()
                .setText(R.string.LOC_Messages)
                .setTabListener(tabListener));

        ...
    }
}

菜单文件的一个例子:

An example of the menu file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item
        android:id="@+id/contacts_action_add"
        android:icon="@drawable/image__white__plus"
        android:showAsAction="always"
        android:title="@string/LOC_Create">
        <menu>
            <item
                android:id="@+id/contacts_action_add_entry"
                android:icon="@drawable/image__white__person"
                android:showAsAction="always"
                android:title="@string/LOC_Contact"/>
            <item
                android:id="@+id/contacts_action_add_group"
                android:icon="@drawable/image__white__persons"
                android:showAsAction="always"
                android:title="@string/LOC_Group"/>
        </menu>
    </item> 
</menu>

和片断的一个例子:

public class ContactsFragment extends Fragment {
    public ContactsFragment() {
        super();
        setHasOptionsMenu(true);
    }

    ...
    private Menu optionsMenu;
    private MenuItem addEntryMenuItem;
    private MenuItem addGroupMenuItem;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        Log.d("ContactsFragment: onCreateOptionsMenu");
        inflater.inflate(R.menu.menu_main__contacts, menu);
        optionsMenu = menu;

        if (optionsMenu != null) {
            Log.d("ContactsFragment: onCreateOptionsMenu - optionsMenu != null");
            addEntryMenuItem= optionsMenu.findItem(R.id.contacts_action_add_entry);
            addGroupMenuItem= optionsMenu.findItem(R.id.contacts_action_add_group);
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d("ContactsFragment: onOptionsItemSelected");
        if (item == addEntryMenuItem) {
            ...
            return true;
        } else if (item == addGroupMenuItem) {
            ...
            return true;
        } 

        return false;
    }
}

我发现,处理问题的动作条的项目,但他们都refere与基数较低的SDK应用程序,4.0之前的几个条目。它seeems从使用硬件菜单按钮动作条菜单转换的应用程序时,也出现了问题。由于我的应用程序支持SDK 14+仅此不能在我的情况的解决方案。

I found several entries that deal with problems with ActionBar items but they all refere to apps with lower base SDKs, prior to 4.0. It seeems that there have been problems when transitioning apps from using the hardware menu button ActionBar menus. Since my app support SDK 14+ only this cannot be the solution in my case.

用户日志显示,该片段的 onCreateOptionsMenu 方法被调用,而且menues正确充气。那么,为什么不能正确显示的菜单项?

User logs show, that the onCreateOptionsMenu method of the fragments is called and that the menues are inflated correctly. So why are the menu items not displayed correctly?

同样的code工作没有在浪费广大的所有设备中的任何问题,只有少数用户的影响。据我可以告诉他们都采用Android 4.1.2。除了这个,我看不到任何的设备相似,等等。

The same code works without any problem on the waste majority of all devices, only a few users are effected. As far as I can tell they all use Android 4.1.2. Beside this I cannot see any similarities in devices, etc.

不知道如何这个问题是可以解决的?

Any idea how this problem can be solved?

PS:阉`机器人:showAsAction =总是``或不使用的菜单项不influende问题

PS: Wether `android:showAsAction="always"`` is used or not for the menu items does not influende the problem.

推荐答案

好像你已经错过了来电的 setHasOptionsMenu 中的onCreate()

Seems like you have missed the call setHasOptionsMenu in onCreate() of your Fragment

请参阅此指南

这篇关于动作条的菜单项上的一些安卓4.1.2设备不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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