简体   繁体   中英

Hide item in ActionBar in Activity with Fragments in ViewPager

Requirements - I have an app with 3 fragments wrapped in ViewPager. There is a Toolbar in Activity, which has 2 menu items and I want to show one of them only when the 2nd fragment is selected.

Problem - My code throws NullPointerException

Stacktrace:

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.

And this is my 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);
    }

Can anyone help me to solve the issue? Thank you

Override activity's onPrepareOptionsMenu(Menu menu) and put the hiding/showing logic there.

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

Then call activity's invalidateOptionsMenu() each time different fragment shows up ( see ViewPager.OnPageChangeListener ). It will trigger 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) {
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM