我在做一个项目(app),主界面底部是4个按钮,点击每个按钮切换到不同的fragment,我采用的是将4个fragment放到一个activity中,然后将每个fragment与相应的button一一对应。当我都设置好后,运行app后,发现fragment都没加载,只有那个主界面的activity出来了,下面四个按钮都点不了。我调了一上午,原来是在主界面activity的onCreate()方法中,有一个super.onCreate(bundle),我把这句代码放到了控件初始化以及监听器的最后面,难怪会出错,如下所示
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);//这个必须放在此方法的第一行,否则布局无法加载
try {
setContentView(R.layout.main_fragment);
setupView();
addListener();
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
//我原来把那个super.onCreate(arg0);放在这一行,肯定会出错了,难怪所有的fragment都不加载
}
以下是主界面加载4个fragment的代码:
public class MainFragmentActivity extends FragmentActivity{
SportFragment sportFragment;
DiscoverFragment discoverFragment;
MessageFragment messageFragment;
MeFragment meFragment;
Button []btnArray=new Button[4];
Fragment []fragmentArray=null;
int currentIndex=0;//当前显示的fragment
int selectedIndex;//选中的button,显示下一个fragment
@Override
protected void onCreate(Bundle arg0) {
//这个必须放在此方法的第一行,否则布局无法加载
super.onCreate(arg0);
try {
setContentView(R.layout.main_fragment);
setupView();
addListener();
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
private void addListener() {
MyButtonListener listener=new MyButtonListener();
for(int i=0;i<btnArray.length;i++){
btnArray[i].setOnClickListener(listener);
}
}
private void setupView() {
btnArray[0]=(Button) findViewById(R.id.btn_main_fragment_sport);
btnArray[1]=(Button) findViewById(R.id.btn_main_fragment_discover);
btnArray[2]=(Button) findViewById(R.id.btn_main_fragment_message);
btnArray[3]=(Button) findViewById(R.id.btn_main_fragment_me);
btnArray[0].setSelected(true);
sportFragment=new SportFragment();
discoverFragment=new DiscoverFragment();
messageFragment=new MessageFragment();
meFragment=new MeFragment();
fragmentArray=new Fragment[]{sportFragment,discoverFragment,messageFragment,meFragment};
//刚开始,显示第一个fragment
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.fragment_container, sportFragment);
transaction.show(sportFragment);
transaction.commit();
}
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.btn_main_fragment_sport:
selectedIndex=0;
break;
case R.id.btn_main_fragment_discover:
selectedIndex=1;
break;
case R.id.btn_main_fragment_message:
selectedIndex=2;
break;
case R.id.btn_main_fragment_me:
selectedIndex=3;
break;
}
//判断点击的是不是当前的
if(selectedIndex!=currentIndex){
//不是当前的
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
//将当前的隐藏
transaction.hide(fragmentArray[currentIndex]);
//并显示你选中的
if(!fragmentArray[selectedIndex].isAdded()){
//以前没添加过
transaction.add(R.id.fragment_container,fragmentArray[selectedIndex]);
}
transaction.show(fragmentArray[selectedIndex]);
transaction.commit();
btnArray[currentIndex].setSelected(false);
btnArray[selectedIndex].setSelected(true);
currentIndex=selectedIndex;
}
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
}
}}