Reputation: 1639
I have the slider menu displayview method as:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
FragmentManager fm = getSupportFragmentManager();
String tag = fragment.getTag(); // instance method of a to get a tag
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, fragment, tag);
ft.addToBackStack(tag);
ft.commit();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
drawerListView.setSelection(position);
setTitle(navMenuTitles[position]);
drawer.closeDrawer(drawerListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I have a fragment called home_fragment whose code behind is HomeFragment.java
When I click on this fragment first it works fine, but on clicking next from the siding menu at index 0 I get an error as:
Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference
What am I doing wrong? Thanks.
This is where I'm having error:
case OP_REMOVE: {
Fragment f = op.fragment;
f.mNextAnim = exitAnim;
mManager.removeFragment(f, transition, transitionStyle);
} break;
This code is present in BackStackRecord.java
Upvotes: 1
Views: 219
Reputation: 31015
You shouldn't be calling container.removeAllViewsInLayout()
.
The FragmentManager
will handle all the view manipulation on the container when you call FragmentTransaction.replace()
, including removing the current fragment view. You are probably getting an error because the FragmentManager
expects a fragment's view to be in the container, but you've removed it.
All you need is this:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
// set fragment to other nav target
fragment = ...
break;
.
.
.
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
drawerListView.setSelection(position);
setTitle(navMenuTitles[position]);
drawer.closeDrawer(drawerListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
Also I noticed you are using getFragmentManager()
. Please check your class imports and make sure that you have the right call.
HomeFragment
extends android.app.Fragment
, use getFragmentManager()
.HomeFragment
extends android.support.v4.app.Fragment
, use getSupportFragmentManager()
.Upvotes: 1