I have an Application with a Navigation View. I haven't had any problem in most of the mobiles I have tested the application with, but it crashes when it's installed in a "OnePlus One" mobile model with Android version 5.x, when I click into a MenuItem in the Navigation View.
I have taken a look and I get a "menuItem -> null" when I click on any item. (navigationView is not null)
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
selectItem(menuItem);
return false;
}
});
When I select an option:
if (menuItem.getTitle() == getResources().getString(R.string.information))
{
fragment = new InformationFragment().newInstance();
title = getString(R.string.information);
} else if (menuItem.getTitle() == getResources().getString(R.string.coaches))
{
fragment = new CoachesFragment().newInstance();
title = getString(R.string.coaches);
} else if (menuItem.getTitle() == getResources().getString(R.string.my_teams))
{
fragment = new TeamsFragment().newInstance();
title = getString(R.string.my_teams);
} else if (menuItem.getTitle() == getResources().getString(R.string.all_players))
{
fragment = new AllPlayersFragment().newInstance();
title = getString(R.string.all_players);
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container_body, fragment)
.commit();
With the next dependencies:
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:palette-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
Environment:
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId 'xxx.xxxx.xxxx'
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName '1.0.2'
I have researched into the net but I haven't found the solution, also I have changed it to 23.1.1 but nothing...
Log:
Fatal Exception: java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:414)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:449)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)
at com.rcassociats.tsunamis.activity.MainActivity.selectItem(MainActivity.java:223)
at com.rcassociats.tsunamis.activity.MainActivity.access$000(MainActivity.java:48)
at com.rcassociats.tsunamis.activity.MainActivity$2.onNavigationItemSelected(MainActivity.java:186)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:84)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:308)
at android.view.View.performClick(View.java:4789)
at android.view.View$PerformClick.run(View.java:19881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
MainActivity.java <--- COMPLETE CODE