SherlockFragmentActivity 选项卡 onBackPressed()

SherlockFragmentActivity tabs onBackPressed()

具有 3 个选项卡的 SherlockFragmentActivity 如何在所有选项卡中添加 onBackPressed()?有可能的?此代码是 http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/ 的默认 abs Viewpage totorial。有帮助吗?

MainActivity.jav

public class MainActivity extends SherlockFragmentActivity {
private FragmentTab2 F2;
ActionBar mActionBar;
ViewPager mPager;
Tab tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            mActionBar.setSelectedNavigationItem(position);
        }
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
    mPager.setAdapter(viewpageradapter);


    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            mPager.setCurrentItem(tab.getPosition());
        }

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

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

    tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
    mActionBar.addTab(tab);

    tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
    mActionBar.addTab(tab);

    tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
    mActionBar.addTab(tab);

}
@Override
public void onBackPressed()
{
    super.onBackPressed();
    F2.onBackPressed();
}
}

ViewPagerAdapter.java

 public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int arg0) {
    switch (arg0) {

    case 0:
        FragmentTab1 fragmenttab1 = new FragmentTab1();
        return fragmenttab1;
    case 1:
        FragmentTab2 fragmenttab2 = new FragmentTab2();
        return fragmenttab2;
    case 2:
        FragmentTab3 fragmenttab3 = new FragmentTab3();
        return fragmenttab3;
    }
    return null;
}

@Override
public int getCount() {
    return PAGE_COUNT;
}

}

FragmentTab2.java

 public class FragmentTab2 extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmenttab2, container, false);
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setUserVisibleHint(true);
}

public boolean onBackPressed() {

    Toast.makeText(getActivity(), "this is my Toast message!!! =)",
               Toast.LENGTH_LONG).show();
    return false;


}

}

错误

 Exception dispatching finished signal.
 Exception in MessageQueue callback: handleReceiveCallback
 java.lang.NullPointerException
 at com.androidbegin.absviewpagertutorial.MainActivity.onBackPressed(MainActivity.java:87)
 at android.app.Activity.onKeyUp(Activity.java:2193)
 at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
 at android.app.Activity.dispatchKeyEvent(Activity.java:2423)
 at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:121)

使用onPageSelected检查用户是否在FragmentTab2然后初始化F2为:

    public void onPageSelected(int position) {
        super.onPageSelected(position);
        mActionBar.setSelectedNavigationItem(position);
        if(position==1)
         F2 = (FragmentTab2)viewpageradapter.getItem(position);
        else
         F2 =null;  
    }

并在 Activity 的 onBackPressed 中进行空检查:

@Override
public void onBackPressed()
{
    super.onBackPressed();
    if(F2 !=null)
      F2.onBackPressed();
}