在选定的选项卡方法中设置当前项目时,应用程序崩溃

时间:2014-07-05 10:42:11

标签: android android-tabs

我遇到了问题,当我尝试在Tab选择的方法中使用设置的当前项目(arg0.get Position())时,我的应用程序崩溃,同时实现我点击选项卡的功能,下面的片段也应该更改用它.....它的另一种方式就是当我滑动时,Tabs随之改变。任何形式的帮助都会很明显..提前谢谢。代码如下:

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AboutUs extends FragmentActivity implements TabListener {

    private ActionBar actionBar;
    ViewPager viewPager;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutus);
        intialization();

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void intialization() {
        // TODO Auto-generated method stub

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("MUMTAZ ANWAR");
        tab1.setTabListener(this);

        ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("CREDIENTIALS");
        tab2.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);

        viewPager = (ViewPager) findViewById(R.id.pagger);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                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 onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        //Log.d("HAMZA", "onTabReselected at  " + "position" + tab.getPosition()
        //      + "name" + tab.getText());
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        //Log.d("HAMZA", "onTabselected at  " + "position" + tab.getPosition()
            //  + "name" + tab.getText());
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        //Log.d("HAMZA", "onTabUnselected at  " + "position" + tab.getPosition()
        //      + "name" + tab.getText());
    }

}

class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub

        Fragment fragment = null;
        if (arg0 == 0) {

            fragment = new BlankFragment();
        }

        if (arg0 == 1) {

            fragment = new BlankFragmenta();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

1 个答案:

答案 0 :(得分:0)

我在你的代码中找不到片段事务

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment); // pass your fragment as second parameter
transaction.addToBackStack(null);// it is optional if you want to put it in stack

// Commit the transaction
transaction.commit();

如何替换片段。它不见了