在ViewPager的Activity with Fragments中隐藏ActionBar中的项目

时间:2015-11-27 08:19:37

标签: android android-actionbar menuitem

要求 - 我有一个包含在ViewPager中的3个片段的应用。 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.

这是我的活动

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);
    }

有人可以帮我解决这个问题吗?谢谢

1 个答案:

答案 0 :(得分:0)

覆盖活动onPrepareOptionsMenu(Menu menu)并将隐藏/显示逻辑放在那里。

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

每次出现不同的片段(see ViewPager.OnPageChangeListener)时,请致电活动invalidateOptionsMenu()。它会触发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) {
    }
});