我想在方向更改时重新加载包含Fragments的FragmentActivity。片段活动包含3个选项卡,每个选项卡包含片段。我的问题是,当旋转设备时,它会崩溃。请帮忙。
代码在这里:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener{
@覆盖 protected void onRestart(){ super.onRestart();
mContext = this;
try{
switch (selectedPosition) {
case 0:
mViewPager.setCurrentItem(0);
break;
case 1:
mViewPager.setCurrentItem(0);
break;
}
}catch(Exception e){
Logger.writeLOG(LOG_TAG, "@onRestart ", e);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchmap_main);
try{
mContext = this;
mLocatorGlobal = (LocatorGlobal) getApplicationContext();
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setText(listTab[i])
.setTabListener(this));
}
}catch(Exception e){
Logger.writeLOG(LOG_TAG, "@onCreate ", e);
}
}
@覆盖 public void onTabReselected(ActionBar.Tab arg0,FragmentTransaction arg1){
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
actionBar.setSelectedNavigationItem(tab.getPosition());
mViewPager.setCurrentItem(tab.getPosition());
mZoneAdapter.refresh(locationList);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fr) {
// TODO Auto-generated method stub
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
switch (position) {
case 0:
MapSectionFragment mapfrag = (MapSectionFragment) MapSectionFragment.newInstance();
args.putInt("key",position);
mapfrag.setArguments(args);
return mapfrag;
case 1:
ListSectionFragment listfrag = (ListSectionFragment) ListSectionFragment.newInstance();
args.putInt("key",position);
listfrag.setArguments(args);
return listfrag;
}
public static class ListSectionFragment extends Fragment {
View rootView = null;
public static Fragment newInstance() {
return new ListSectionFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try{
if (null == savedInstanceState)
rootView = inflater.inflate(R.layout.listfragment, container,
false);
..........
}
public static class MapSectionFragment extends Fragment {
GoogleMap map;
public static Fragment newInstance() {
return new MapSectionFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try{
if (null == savedInstanceState)
rootView = inflater.inflate(R.layout.lay_search_map, container,
false);
........
}
...............
}