BottomNavigationView setSelectedItemId崩溃

时间:2017-07-10 17:23:25

标签: android android-fragments bottomnavigationview

我正在使用BottomNavigationView和Fragment编写应用程序,以编程方式更新布局。

我正在尝试使用Stack来允许用户单击并返回到上一个片段。我试图在膨胀一个新片段后使用addToBackStack,但是当尽管布局正确改变时按下后退,所选项目菜单也不会随之改变。

所以我决定创建一个Fragment的自定义堆栈,并使用setSelectedItemId更改所选项,但此操作会使应用程序崩溃。我通常花了很多时间,而且我无法理解这种行为。

感谢您的帮助。

public class MainActivity extends FragmentActivity {

private FragmentManager fm = getSupportFragmentManager();
private Logger logger = Logger.getLogger("global");
private BottomNavigationView bnv;
private CustomStack customStack = new CustomStack();

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        Fragment centerFragment;
        switch (item.getItemId()) {

            case R.id.navigation_home:
                centerFragment = new Fragment1();
                replaceCenterLayoutFragment(centerFragment);
                customStack.push(centerFragment);

                return true;
            case R.id.navigation_map:
                centerFragment = new Fragment2();
                bnv.setSelectedItemId(R.id.navigation_interest);
                return true;
            case R.id.navigation_interest:
                centerFragment = new Fragment3();
                replaceCenterLayoutFragment(centerFragment);
                customStack.push(centerFragment);

                return true;
            case R.id.navigation_notifications:
                centerFragment = new Fragment4();
                replaceCenterLayoutFragment(centerFragment);
                customStack.push(centerFragment);

                return true;
        }

        return false;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bnv = (BottomNavigationView) findViewById(R.id.navigation);
    bnv.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    FragmentTransaction ft1 = fm.beginTransaction();
    Board b = new Board();
    ft1.add(R.id.center_layout, new Fragment1()).commit();
    customStack.push(b);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        customStack.pop();
        replaceCenterLayoutFragment(customStack.top());
    }
    return false;
}

private void replaceCenterLayoutFragment(Fragment f){
    FragmentTransaction ft1 = fm.beginTransaction();
    ft1.replace(R.id.center_layout, f).commit();
    syncMenuItem(f);
}

private void syncMenuItem(Fragment f){
    if(f instanceof Fragment1)
        bnv.setSelectedItemId(R.id.p1);
    if(f instanceof Fragment2)
        bnv.setSelectedItemId(R.id.p2);
    if(f instanceof Fragment3)
        bnv.setSelectedItemId(R.id.p3);
    if(f instanceof Fragment4)
        bnv.setSelectedItemId(R.id.p4);
}
}

这是我的自定义堆栈

public class CustomStack {

private ArrayList<Fragment> menu ;
private Logger logger = Logger.getLogger("global");

public CustomStack(){
    menu = new ArrayList<>();
}

public void push(Fragment i){
    menu.add(i);
}

public Fragment pop (){
    if(menu.size() > 0) {
        int s = menu.size() -1;
        Fragment app = menu.get(s);
        menu.remove(s);
        return app;
    }
    else{
        return null;
    }
}

public Fragment top(){
    if(menu.size() > 0)
        return menu.get(menu.size() -1);
    else
        return null;
}

public boolean isEmpty(){
    return menu.size() > 0;
}

 }

和LogCatPage

07-10 20:31:07.879 28384-28384/? I/art: Late-enabling -Xcheck:jni
07-10 20:31:07.970 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.145 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.187 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.224 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.264 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.311 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.347 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.387 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.427 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.463 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.508 28384-28384/com.example.longsky.peoople W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.example.longsky.peoople-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.example.longsky.peoople-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
07-10 20:31:08.509 28384-28384/com.example.longsky.peoople W/System: ClassLoader referenced unknown path: /data/app/com.example.longsky.peoople-2/lib/arm64
07-10 20:31:08.512 28384-28384/com.example.longsky.peoople I/InstantRun: starting instant run server: is main process
07-10 20:31:08.600 28384-28384/com.example.longsky.peoople W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-10 20:31:08.655 28384-28499/com.example.longsky.peoople E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
07-10 20:31:08.655 28384-28499/com.example.longsky.peoople D/AppTracker: App Event: start
07-10 20:31:08.661 28384-28501/com.example.longsky.peoople D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-10 20:31:08.679 28384-28501/com.example.longsky.peoople I/Adreno: QUALCOMM build                   : 183c040, Iff84fb1103
                                                                     Build Date                       : 03/18/16
                                                                     OpenGL ES Shader Compiler Version: XE031.06.00.02
                                                                     Local Branch                     : 
                                                                     Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.BF64.1.2.2_RB4.06.00.01.180.031
                                                                     Remote Branch                    : NONE
                                                                     Reconstruct Branch               : NOTHING
07-10 20:31:08.686 28384-28501/com.example.longsky.peoople I/OpenGLRenderer: Initialized EGL, version 1.4
07-10 20:31:10.516 28384-28384/com.example.longsky.peoople D/AndroidRuntime: Shutting down VM
07-10 20:31:10.592 28384-28384/com.example.longsky.peoople E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.longsky.peoople, PID: 28384
                                                                             java.lang.StackOverflowError: stack size 8MB
                                                                                 at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                                 at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                                 at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                                 at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                 at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                                 at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                                 at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                                 at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                                 at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                 at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                                 at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                                 at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                                 at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                                 at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                 at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                                 at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                                 at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                                 at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                                 at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                 at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                                 at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                                 at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                                 at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                                 at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                                 at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                                 at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                                 at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                                 at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                                at android.support.d
07-10 20:31:10.594 28384-28384/com.example.longsky.peoople D/AppTracker: App Event: crash
07-10 20:31:10.638 28384-28384/com.example.longsky.peoople D/Error: ERR: exClass=java.lang.StackOverflowError
07-10 20:31:10.638 28384-28384/com.example.longsky.peoople D/Error: ERR: exMsg=stack size 8MB
07-10 20:31:10.638 28384-28384/com.example.longsky.peoople D/Error: ERR: file=MainActivity.java
07-10 20:31:10.638 28384-28384/com.example.longsky.peoople D/Error: ERR: class=com.example.longsky.peoople.MainActivity
07-10 20:31:10.638 28384-28384/com.example.longsky.peoople D/Error: ERR: method=replaceCenterLayoutFragment line=96
07-10 20:31:10.647 28384-28384/com.example.longsky.peoople D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
                                                                        at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                        at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                        at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                        at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                        at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                        at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                        at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                        at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                        at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                        at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                        at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                        at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                        at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                        at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                        at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                        at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                        at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                        at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                        at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                        at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                        at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                        at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                        at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                        at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:184)
                                                                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                                                                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
                                                                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
                                                                        at android.support.design.widget.BottomNavigationView.setSelectedItemId(BottomNavigationView.java:341)
                                                                        at com.example.longsky.peoople.MainActivity.replaceCenterLayoutFragment(MainActivity.java:96)
                                                                        at com.example.longsky.peoople.MainActivity.access$000(MainActivity.java:15)
                                                                        at com.example.longsky.peoople.MainActivity$1.onNavigationItemSelected(MainActivity.java:39)
                                                                        at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigati
07-10 20:31:10.647 28384-28384/com.example.longsky.peoople D/Error: ERR: TOTAL BYTES WRITTEN: 862560
07-10 20:31:10.695 28384-28384/com.example.longsky.peoople I/Process: Sending signal. PID: 28384 SIG: 9

3 个答案:

答案 0 :(得分:1)

问题是你setSelectedItemId调用了监听器,该监听器将调用replaceCenterLayoutFragment,它将调用setSelectedItemId,依此类推。

一个hacky修复程序将是以下

private void replaceCenterLayoutFragment(Fragment f){
    FragmentTransaction ft1 = fm.beginTransaction();
    ft1.replace(R.id.center_layout, f).commit();

    bnv.setOnNavigationItemSelectedListener(null);
    syncMenuItem(f); 
    bnv.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

答案 1 :(得分:0)

对于其他在此问题上绊脚石的人。如果通过菜单选择导航项,则不会调用侦听器:

bottomNavBar.getMenu().findItem(R.id.action_open_map).setChecked(true);

答案 2 :(得分:0)

我找到了一个简单得多的解决方案。 BottomNavigation还有另一个名为

的列表器
  

setOnNavigationItemReselectedListener

只需重写该侦听器,并将方法主体保留为空。

bottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
            return;
        }
 });

说明:

我的猜测是 setSelectedItemId 与之前调用 listener 的相同,后者将调用 replaceCenterLayoutFragment ,后者将再次调用 setSelectedItemId strong>,但这一次它已经调用了普通的 SelectedListener ,因此它将调用 ReselectedListener

NavigationItemReselectedListener ,然后仅返回void并结束方法调用循环。