我很难使用ActionBar制作标签。当我通过一些代码更改来运行应用程序时,我的应用程序立即崩溃,甚至没有启动。当前代码最接近我可以获得的工作。这是MainActivity:
import android.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar.Tab;
import android.app.ActionBar;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
FragmentPageAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
mAdapter = new FragmentPageAdapter(getSupportFragmentManager());
//final ActionBar actionBar = getActionBar();
//final ActionBar actionBar = getSupportActionBar();
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("DotNet").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Eal").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Neowin").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("PcWorld").setTabListener(this));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@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;
}
}