5

这两天我正在尝试解决这个问题。我在网上和stackoverflow上搜索过,但我找不到我的问题的答案。

在我的应用程序中,我试图实现两个选项卡。需要显示一张地图,在另一个选项卡中我将显示一个列表适配器。(使用ActionBarSherlock

当我单击第二个选项卡然后单击第一个选项卡时,出现异常:

10-08 01:27:45.203: E/AndroidRuntime(7082): java.lang.IllegalStateException: Activity 已被销毁

这是我的活动代码和我的片段类。(只是为了测试,我把 CustomMapFragment 类放了两次)。

    public class MapActivity extends SherlockFragmentActivity implements    LocationListener,TabListener{

    LatLng mLocation;
    GoogleMap map;

    //CustomMapFragment fragment1 = new CustomMapFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //FragmentManager fm = getSupportFragmentManager();

        if (savedInstanceState == null) {
            setContentView(R.layout.activity_main);
            FragmentManager fm = getSupportFragmentManager();
            setActionBar();
            //fragment2 = new Fragment2(); 

            //FragmentTransaction ft = fm.beginTransaction();
            //ft.add(R.id.container, fragment1, "frag1");

            //ft.add(R.id.fragment_container, fragment2, "frag2");

            //ft.commit();
        }


    }

private void setActionBar() {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        CustomMapFragment frag1 = new CustomMapFragment();
        CustomMapFragment frag2 = new CustomMapFragment();

        Tab tab1 = actionBar.newTab()
                .setText("Frag1")
                .setTabListener(new NavTabListener(frag1));

        actionBar.addTab(tab1);

        Tab tab2 = actionBar.newTab()
                .setText("Frag2")
                .setTabListener(new NavTabListener(frag2));
                actionBar.addTab(tab2);


    }

    protected class NavTabListener implements ActionBar.TabListener {

        private Fragment fragment;

        public NavTabListener(Fragment fragment) {
            this.fragment = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.container, fragment);
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            FragmentManager fm = getSupportFragmentManager();
            ft = fm.beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }

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

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }   

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}

    public class CustomMapFragment extends SherlockFragment{

    private SupportMapFragment fragment;
    private GoogleMap map;

    public CustomMapFragment(){
        super();
    }



    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        View lView = inflater.inflate(R.layout.map_fragment , null);
        return lView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map, fragment).commit();
        }
    }


@Override
public void onResume() {
    super.onResume();
    if (map == null) {
        map = fragment.getMap();
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
    }
}


}

提前致谢!

4

0 回答 0