ActionBar被摧毁了,可能的解决办法是什么?

时间:2015-12-17 07:49:03

标签: android android-fragments

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ActionBar actionBar=getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.addTab(actionBar.newTab().setText("All").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("Not Called").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("Called").setTabListener(this));
    }

    @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) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    if(tab.getPosition()==0){
    FragmentTop fragmentTop = new FragmentTop();
            getSupportFragmentManager().beginTransaction().replace(R.id.container,fragmentTop).commit();
    }else if (tab.getPosition()==1){
    FragmentTop2 fragmentTop2 = new FragmentTop2();
    getSupportFragmentManager().beginTransaction().replace(R.id.container,fragmentTop2).commit();
    }else if (tab.getPosition()==2){
    FragmentTop3 fragmentTop3 = new FragmentTop3();
    getSupportFragmentManager().beginTransaction().replace(R.id.container,fragmentTop3).commit();
    }
}

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
   }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    toolbar.setTitle(R.string.app_name);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     getSupportActionBar().setDisplayShowHomeEnabled(true);
     getSupportActionBar().setDisplayShowTitleEnabled(true);
}
tabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),android.R.id.tabcontent);
bundle=new Bundle();
// to send data u can send it by binding it with bundle
tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Tab1"),
        ShowStatusFragment.class,bundle);

tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Tab2"),
        ShowStatusFragment.class,bundle);

tabHost.addTab(tabHost.newTabSpec("Tab3").setIndicator("Tab3"),
        ShowStatusFragment.class,bundle);

答案 1 :(得分:0)

您现在应该使用Toolbar带来的AppCompat v7。 (compile 'com.android.support:appcompat-v7:23.1.0'

答案 2 :(得分:0)

您需要使用android.support.v7.widget.Toolbar AppCompat7

compile 'com.android.support:appcompat-v7:23.1.0'

添加您的Gradle模块应用