I am trying to implement a navigation drawer. "Fragment1" is a list fragment:
public class Fragment1 extends ListFragment {
private List<String> musicList;
public static ListFragment newInstance(Context context) {
Fragment1 f = new Fragment1();
return f;
}
However i cannot use the fragmenttransaction to replace the fragments in the main activity:
public class MainActivity extends AbstractNavDrawerActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Fragment1()).commit();
}
}
@Override
protected void onNavItemSelected(int id) {
switch ((int)id) {
case 101:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Fragment1()).commit();
break;
case 102:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Fragment2()).commit();
break;
}
}
}
The replace method has a redline under it and says:
The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment1)"
What should i do?
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Fragment1()).commit();
IMO you should avoid having such long code with method call after method code, it's messy and hard to understand! Maybe try :
FragmentTransaction transaction = getActivity().getSupportFragmentManager()
.beginTransaction();
Fragment newFragment = new Fragment1();
transaction.add(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
transaction.commit();