如何在更改选项卡上隐藏菜单项?

时间:2014-03-18 06:13:32

标签: android android-actionbar android-tabs android-menu

我有2个选项卡,而选择第一个选项卡时,接受菜单图标不可见。 选择第二个选项卡时,接受菜单图标应该可见。我试过,但我得到空指针异常。 并且还应该在选择接受菜单时从第二个活动中获取文本。

我该怎么做?

这是我的代码:

public class DetailTabActivity extends TabActivity implements OnTabChangeListener  {
    String name,address,rating,reference,lat,lng;
    StringConstants constants;
    Float rate;
    MenuItem menuitem;
    String url;
     TabHost tabHost;

     Menu menu1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.overridePendingTransition(R.anim.anim_out,
                R.anim.anim_in);
        constants=new StringConstants();
        setContentView(R.layout.layout_detail_tab);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        Log.e("name",name+"");
        address = intent.getStringExtra("address");
        Log.e("address",address+"");
        rating = intent.getStringExtra("rating");
        Log.e("rating",rating+"");
        reference = intent.getStringExtra("reference");
        Log.e("reference",reference+"");
        lat = intent.getStringExtra("lat");
        Log.e("lat",lat+"");
        lng = intent.getStringExtra("lng");
        Log.e("lng",lng+"");
        ActionBar actionBar = getActionBar();
        // Enabling Up / Back navigation
        actionBar.setDisplayHomeAsUpEnabled(true);
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(this);
        // Tab for Photos
        TabSpec detailspec = tabHost.newTabSpec("Detail");
        // setting Title and Icon for the Tab
        detailspec.setIndicator("Detail");
        Intent detailintent = new Intent(this, DetailActivity.class);
        detailintent.putExtra("name",name);
        detailintent.putExtra("address",address);
        detailintent.putExtra("rating",rating);
        detailintent.putExtra("reference",reference);
        detailspec.setContent(detailintent);

        // Tab for Songs
        TabSpec reviewspec = tabHost.newTabSpec("Review");       
        reviewspec.setIndicator("Review/Rating");
        Intent songsIntent = new Intent(this, ReviewAndRating.class);
        songsIntent.putExtra("name",name);
        songsIntent.putExtra("lat",lat);
        songsIntent.putExtra("lng",lng);
        reviewspec.setContent(songsIntent);



        // Adding all TabSpec to TabHost
        tabHost.addTab(detailspec); // Adding photos tab
        tabHost.addTab(reviewspec); // Adding songs tab

        tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#ffffff"));


        // Set Tab1 as Default tab and change image  
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#134960"));






}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.layout_detail_tab, menu);
    menuitem = menu.findItem(R.id.action_accept);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
     case R.id.action_accept:

          return true;
          default:
          return super.onOptionsItemSelected(item);
  }
}
@Override
public void onTabChanged(String tabId) {


    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        if(i==0)
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
        else if(i==1)
             tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
    }


    Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());

if(tabHost.getCurrentTab()==1)
{  
    menuitem.setVisible(true);
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
    menuitem.setVisible(false);
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}


}
}

1 个答案:

答案 0 :(得分:0)

设置invalidateOptionsMenu(); as,

    if(tabHost.getCurrentTab()==1)
      {  
      menuitem.setVisible(true);
      invalidateOptionsMenu();
                               tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
      }
    else if(tabHost.getCurrentTab()==0)
{
  menuitem.setVisible(false);
      invalidateOptionsMenu();
       tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
 }