使用 ViewPager 中的 Fragments 在 Activity 中隐藏 ActionBar 中的项目

Hide item in ActionBar in Activity with Fragments in ViewPager

要求 - 我有一个包含 3 个片段的应用程序,这些片段包含在 ViewPager 中。 Activity 中有一个工具栏,它有 2 个菜单项,我只想在选择第二个片段时显示其中一个。

问题 - 我的代码抛出 NullPointerException

堆栈跟踪:

11-27 15:10:01.940 25899-25899/com.example.michael.ims_application_apps E/AndroidRuntime: java.lang.NullPointerException
11-27 15:10:01.940 25899-25899/com.example.michael.ims_application_apps E/AndroidRuntime:     at com.example.michael.ims_application_apps.MainActivity.onCreateOptionsMenu(MainActivity.java:67)
11-27 15:10:01.940 25899-25899/com.example.michael.ims_application_apps E/AndroidRuntime:     at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
11-27 15:10:01.940 25899-25899/com.example.michael.ims_application_apps E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
11-27 15:10:01.940 25899-25899/com.example.michael.ims_application_apps E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
11-27 15:10:01.

这是我的 activity

public class MainActivity extends AppCompatActivity
{
    private ViewPager pager;
    private AdapterFragment mAdapter;
    private PageIndicator mIndicator;
    private String name;
    MyTimer tim;

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

        android.support.v7.widget.Toolbar myToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.ic_launcher);

        // Pada activity ini akan make AdapterFragment
        mAdapter = new AdapterFragment(getSupportFragmentManager());

        // Untuk ViewPagerIndicator
        pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(mAdapter);

        // TitlePageIndicator merupakan class di library ViewPagerIndicator
        mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(pager);

        if(pager.getCurrentItem() != 1)
        {
            MenuItem sync = (MenuItem) findViewById(R.id.action_sync);
            sync.setVisible(false);
        }

        //Hidupin session waktu N.B: Waktu boleh berapa lama apps ke-pause secara pastinya belum diset !!!!
//        tim = new MyTimer(50000,10);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();



        // set OptionButton yang di device
        switch (id)
        {
            case R.id.action_sync:
                if(pager.getCurrentItem() != 1)
                {
                    Log.d("Tag", pager.getCurrentItem()+"");
                    setVisible(false);
                }
                Toast.makeText(this, "Sync", Toast.LENGTH_LONG).show();
                return true;

            case R.id.Logout:
                SharedPreferences setting = getSharedPreferences("USER", 0);
                SharedPreferences.Editor editor = setting.edit();
                editor.putString("userName", "-1");
                editor.commit();

                Intent i = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(i);
                finish();
        }

        return super.onOptionsItemSelected(item);
    }

谁能帮我解决这个问题?谢谢

覆盖 activity 的 onPrepareOptionsMenu(Menu menu) 并将 hiding/showing 逻辑放在那里。

public boolean onPrepareOptionsMenu(Menu menu) {
  MenuItem item = menu.findItem(R.id.action_sync);
  item.setVisible(pager.getCurrentItem() == 1);
  return true;
}

然后每次出现不同的片段时调用 activity 的 invalidateOptionsMenu() (see ViewPager.OnPageChangeListener)。会触发onPrepareOptionsMenu(Menu menu).

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        invalidateOptionsMenu();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});