想问一下我是按照老师的方法写了,当时为什么每次都会闪退掉啊!哪怕单独运行mainactivity也会闪退
public class MainActivity extends FragmentActivity implements OnClickListener
{
private LinearLayout mtab_mainactivity_main;
private LinearLayout mtab_mainactivity_load;
private LinearLayout mtab_mainactivity_user;
private ImageButton mimgbt_mainactivity_main;
private ImageButton mimgbt_mainactivity_load;
private ImageButton mimgbt_mainactivity_user;
private Fragment mTab01;
private Fragment mTab02;
private Fragment mTab03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initEvent();
setSelect(0);
}
private void initEvent() {
mtab_mainactivity_main.setOnClickListener(this);
mtab_mainactivity_load.setOnClickListener(this);
mtab_mainactivity_user.setOnClickListener(this);
}
private void initView() {
mtab_mainactivity_main = (LinearLayout)findViewById(R.id.tab_mainactivity_main);
mtab_mainactivity_load = (LinearLayout)findViewById(R.id.tab_mainactivity_load);
mtab_mainactivity_user = (LinearLayout)findViewById(R.id.tab_mainactivity_user);
mimgbt_mainactivity_main = (ImageButton)findViewById(R.id.imgbt_mainactivity_main);
mimgbt_mainactivity_load = (ImageButton)findViewById(R.id.imgbt_mainactivity_load);
mimgbt_mainactivity_user = (ImageButton)findViewById(R.id.imgbt_mainactivity_user);
}
private void setSelect(int i) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
hideFragment(transaction);
switch (i)
{
case 0:
if (mTab01==null)
{
mTab01 = new MainFragment();
transaction.add(R.id.id_content,mTab01);
}else
{
transaction.show(mTab01);
}
mimgbt_mainactivity_main.setImageResource(R.drawable.imgbt_mainactivity_main_press);
break;
case 1:
if (mTab02==null)
{
mTab02 = new LoadFragment();
transaction.add(R.id.id_content,mTab02);
}else
{
transaction.show(mTab02);
}
mimgbt_mainactivity_load.setImageResource(R.drawable.imgbt_mainactivity_load_press);
break;
case 2:
if (mTab03==null)
{
mTab03 = new UserFragment();
transaction.add(R.id.id_content,mTab03);
}else
{
transaction.show(mTab03);
}
mimgbt_mainactivity_user.setImageResource(R.drawable.imgbt_mainactivity_user_press);
break;
default:
break;
}
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction) {
if (mTab01 !=null)
{
transaction.hide(mTab01);
}
if (mTab02 != null)
{
transaction.hide(mTab02);
}
if (mTab03 != null)
{
transaction.hide(mTab03);
}
}
@Override
public void onClick(View v)
{
resetImgs();
switch (v.getId())
{
case R.id.tab_mainactivity_main:
setSelect(0);
break;
case R.id.tab_mainactivity_load:
setSelect(1);
break;
case R.id.tab_mainactivity_user:
setSelect(2);
break;
default:
break;
}
}
private void resetImgs() {
mimgbt_mainactivity_main.setImageResource(R.drawable.imgbt_mainactivity_main);
mimgbt_mainactivity_load.setImageResource(R.drawable.imgbt_mainactivity_load);
mimgbt_mainactivity_user.setImageResource(R.drawable.imgbt_mainactivity_user);
}
}