0

我有一个带有导航视图的应用程序。在我测试过该应用程序的大多数手机中,我都没有遇到任何问题,但是当我在导航视图中单击 MenuItem 时,它安装在 Android 版本 5.x 的“OnePlus One”手机模型中时会崩溃.

我看了看,当我单击任何项​​目时,我得到一个“ menuItem -> null ”。(navigationView 不为空)

 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
    {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem)
        {
            selectItem(menuItem);
            return false;
        }
    });

当我选择一个选项时:

 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();

使用下一个依赖项:

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'

环境:

compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
    applicationId 'xxx.xxxx.xxxx'
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName '1.0.2'

我已经研究过网络但我还没有找到解决方案,我也将其更改为 23.1.1 但没有...

日志:

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 <--- 完整代码

4

0 回答 0