java.lang.IllegalStateException:已添加片段:

java.lang.IllegalStateException: Fragment already added:(java.lang.IllegalStateException:已添加片段:)
本文介绍了java.lang.IllegalStateException:已添加片段:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 TabLayout 的 ViewPager.我通过扩展 FragmentStatePagerAdapter 创建了 ViewPagerAdapter.当我刷 ViewPager 时出现以下错误

I have a ViewPager with a TabLayout. I have created ViewPagerAdapter by extending FragmentStatePagerAdapter. When i swipe ViewPager i got the following error

 FATAL EXCEPTION: main
                                                                   Process: com.anubavam.creatrix, PID: 21388
                                                                   java.lang.IllegalStateException: Fragment already added: AttendanceViewFragment

这里是 ViewPagerAdapter 类代码

Here is ViewPagerAdapter Class code

 private class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<AttendanceViewFragment> mFragments = new ArrayList<>();

    private ArrayList<String> buttons = new ArrayList<>();

    ViewPagerAdapter(FragmentManager fm, ArrayList<String> actionButtons) {
        super(fm);
        this.buttons = actionButtons;

    }

    void addFragment(AttendanceViewFragment frag) {
        mFragments.add(frag);
    }

    @Override
    public Fragment getItem(int position) {
        return new AttendanceViewFragment();
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return buttons.get(position);
    }
}

这是我将片段添加到 ViewPager 适配器的方式:

This is the way i added Fragments into ViewPager Adapter:

  adapter = new ViewPagerAdapter(getSupportFragmentManager(), titles);

    for (int i = 0; i < titles.size(); i++) {

        //order of the fragment
        if (i == 0) {
            adapter.addFragment(AttendanceViewFragment.newInstance(studentList.toString(), "T"));
        } else if (i == 1) {
            adapter.addFragment(AttendanceViewFragment.newInstance(present.toString(), "P"));
        } else if (i == 2) {
            adapter.addFragment(AttendanceViewFragment.newInstance(absent.toString(), "A"));
        } else if (i == 3) {
            adapter.addFragment(AttendanceViewFragment.newInstance(late.toString(), "L"));
        }
    }

    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);

我尝试使用 FragmentPagerAdapter 而不是 FragmentStatePagerAdapter.但是没有用.建议我解决此问题的任何其他方法.

I tried with FragmentPagerAdapter instead of FragmentStatePagerAdapter. But no use. Suggest me any other approach to solve this issue.

推荐答案

另一方面,您可以通过以下代码使用 viewpager2:

In other hand you can use viewpager2 with this code:

myPagerAdapter.kt

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter


class PagerAdapterAccounting internal constructor(fm: FragmentActivity) : FragmentStateAdapter(fm) {

    override fun getItemCount(): Int {
        return mFragmentList.size
    }

    override fun createFragment(position: Int): Fragment {
        return mFragmentList[position]
    }

    private val mFragmentList = ArrayList<Fragment>()
    private val mFragmentTitleList = ArrayList<String>()


    fun addFragment(fragment: Fragment, title: String) {
        mFragmentList.add(fragment)
        mFragmentTitleList.add(title)
    }

    fun getTabTitle(position: Int): String {
        return mFragmentTitleList[position]
    }
}

用户界面:

 <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorPrimaryDark"
        app:tabIndicatorHeight="2dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorPrimary" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

及用法:

val fragmentAdapter = PagerAdapterAccounting(this) //must be a fragmentActivity

fragmentAdapter.addFragment(SampleFragment.newInstance(), "theSampleTitle1")
fragmentAdapter.addFragment(SampleFragment2.newInstance(), "theSampleTitle2")

viewPager.adapter = fragmentAdapter

TabLayoutMediator(tabLayout, viewPager,
                TabLayoutMediator.TabConfigurationStrategy { tab, position -> tab.text =  fragmentAdapter.getTabTitle(position) }).attach()

别忘了添加 appcompatX &材料库:

dont forget to add appcompatX & material lib:

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha02'

希望能解决您的问题.

这篇关于java.lang.IllegalStateException:已添加片段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)