在我的FragmnetActivity类中,我有3个fragmnet的Fragmnet Transaction。 但是我的onclick方法它没有正常工作。如果条件不起作用,只为一个if和else condition.my其他工作。
我在这里做错了什么..
public class Indi_Add_Details_menu extends FragmentActivity{
Fragment fragment;
Button btnFragment1, btnFragment2, btnFragment3, btnFragment4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_detal_menu);
btnFragment1 = (Button)findViewById(R.id.btn1);
btnFragment2 = (Button)findViewById(R.id.btn2);
btnFragment3 = (Button)findViewById(R.id.btn3);
btnFragment4 = (Button)findViewById(R.id.btn4);
btnFragment1.setBackgroundResource(R.drawable.add_sbutton_pressed);
btnFragment2.setBackgroundResource(R.drawable.add_tbutton_pressed);
btnFragment3.setBackgroundResource(R.drawable.add_pbutton_pressed);
btnFragment4.setBackgroundResource(R.drawable.view_button_pressed);
// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
Fragment_addStudentDetails myFragment = new Fragment_addStudentDetails();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();
btnFragment1.setOnClickListener(btnFragmentOnClickListener);
btnFragment2.setOnClickListener(btnFragmentOnClickListener);
btnFragment3.setOnClickListener(btnFragmentOnClickListener);
btnFragment4.setOnClickListener(btnFragmentOnClickListener);
}
Button.OnClickListener btnFragmentOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
Fragment newFragment;
// Create new fragment
if(v == btnFragment1){
newFragment = new Fragment_addStudentDetails();
}if(v == btnFragment2){
newFragment = new Group_Add_test();
}else{
newFragment = new ExpandableListFragment();
}
// Create new transaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}};
public void showDatePickerDialog(View v) {
new DatePickerFragment((EditText) v).show(getFragmentManager(), "datePicker");
}
}
当我添加那样的Switch语句时
switch (v.getId()){
case R.id.btn1:
newFragment = new Fragment_addStudentDetails();
break;
case R.id.btn2:
newFragment = new Group_Add_test();
break;
case R.id.btn3:
newFragment = new ExpandableListFragment();
break;
default:
break;
}
logcat说,
Error:(80, 50) error: variable newFragment might not have been initialized
答案 0 :(得分:0)
从代码中我可以看出每次 newFragment = new ExpandableListFragment(); 必须执行。 你在两种情况检查中都犯了错误。 if(v == btnFragment1) ....不是检查视图是否被点击的正确方法....因为它将比较引用。 解决方案: if(v.getId()== btnFragment1.getId()){...
答案 1 :(得分:0)
Fragment newFragment;
之前初始化您的片段
答案 2 :(得分:0)
这个工作
@Override
public void onClick(View v) {
Fragment newFragment;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (v.getId()){
case R.id.btn1:
newFragment = new Fragment_addStudentDetails();
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case R.id.btn2:
newFragment = new Group_Add_test();
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case R.id.btn3:
newFragment = new ExpandableListFragment();
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}};