如何在Android中向选项卡添加片段?

如何在Android中向选项卡添加片段?,android,android-fragments,Android,Android Fragments,这是我的地图片段: public class MapFragment extends SherlockFragment { public static final String TAG = "mapFragment"; public MapFragment() {} @Override public void onCreate(Bundle arg0) { super.onCreate(arg0); setRetainInst

这是我的地图片段:

public class MapFragment extends SherlockFragment {

    public static final String TAG = "mapFragment";

    public MapFragment() {}

    @Override
    public void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
        // The Activity created the MapView for us, so we can do some init stuff.
        Exchanger.mMapView.setClickable(true);
        Exchanger.mMapView.setBuiltInZoomControls(true); // If you want.

        /*
         * If you're getting Exceptions saying that the MapView already has
         * a parent, uncomment the next lines of code, but I think that it
         * won't be necessary. In other cases it was, but in this case I
         * don't this should happen.
         */
        /*
         * final ViewGroup parent = (ViewGroup) Exchanger.mMapView.getParent();
         * if (parent != null) parent.removeView(Exchanger.mMapView);
         */

        return Exchanger.mMapView;
    }
}






This is my LocationTabActivity: 




public class LocationTabActivity extends RoboSherlockMapActivity implements ActionBar.TabListener{



    private MapFragment mMapFragment;
    private MyListFragment mMyListFragment;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Exchanger.mMapView = (MapView)findViewById(R.id.mapView1);


        getSupportActionBar()

          .setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



    ActionBar.Tab newTab0 = getSupportActionBar().newTab();

    newTab0.setText("Location");

    ActionBar.Tab newTab1 = getSupportActionBar().newTab();

    newTab1.setText("Map");

    newTab0.setTabListener(this);

    newTab1.setTabListener( this);

    getSupportActionBar().addTab(newTab0);

    getSupportActionBar().addTab(newTab1);




    }
我发现很难将片段添加到Android选项卡。有什么帮助吗

这可能会对你有所帮助

您还可以在API级别低于11的情况下实现片段。为此,您需要执行以下步骤:-将android-support-v4.jar添加到您的项目中。在SDK-PATH/extras/android/compatibility/v4/上找到这个jar。将其复制到项目根目录中的libs文件夹中,并将其添加到项目生成路径中。使用FragmentActivity而不是Activity。要获取FragmentManager,请使用getSupportFragmentManager而不是getFragmentManager