当前位置: 首页 > 工具软件 > Lang switcher > 使用案例 >

错误解决:java.lang.IllegalStateException: Can't change tag of fragment TransferFragment

郭胤
2023-12-01

错误信息: java.lang.IllegalStateException: Can't change tag of fragment TransferFragment{6e857a4 (3f474cec-7622-4fbd-aeb3-b975f87bbc15) id=0x7f0800ea android:switcher:2131230954:0}: was android:switcher:2131230954:0 now android:switcher:2131230954:1

 

出错代码:

        viewPager = findViewById(R.id.viewpager);
        TransferFragment transferFragment = new TransferFragment();
        final List<Fragment> fragments = new ArrayList<>();
        fragments.add(transferFragment);
        fragments.add(transferFragment);
        fragments.add(transferFragment);

 

原因分析:list add的是同一个fragment对象,所以导致pageadapter抛出无法识别切换fragment

解决:实例化多个对象即可

        viewPager = findViewById(R.id.viewpager);
        TransferFragment transferFragment = new TransferFragment();
        TransferFragment transferFragment1 = new TransferFragment();
        TransferFragment transferFragment2 = new TransferFragment();
        final List<Fragment> fragments = new ArrayList<>();
        fragments.add(transferFragment);
        fragments.add(transferFragment1);
        fragments.add(transferFragment2);

 

 类似资料: