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

在我的MyPagerAdapter类上已经添加了错误片段,它扩展了FragmentPagerAdapter

这是我的错误日志

E / AndroidRuntime:致命异常:main     进程:id.cahyowhy.tmdbmovieimplementation,PID:8228     java.lang.IllegalStateException:片段已添加:MovieFragment {df79747}(01cf33b8-9eb3-4dce-9519-384ca7dd7570)id = 0x7f08014e android:switcher:2131231054:0}         在androidx.fragment.app.FragmentStore.addFragment(FragmentStore.java:67)         在androidx.fragment.app.FragmentManager.addFragment(FragmentManager.java:1563)         在androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:405)         在androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2167)         在androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1990)         在androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1945)         在androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)         在androidx.fragment.app.FragmentManager $ 4.run(FragmentManager.java:413)         在android.os.Handler.handleCallback(Handler.java:873)         在android.os.Handler.dispatchMessage(Handler.java:99)         在android.os.Looper.loop(Looper.java:193)         在android.app.ActivityThread.main(ActivityThread.java:6669)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)

这是我的pageradapter代码

class MyPagerAdapter(fm: FragmentManager) :
    FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val pages = listOf(
        MovieFragment.newInstance(MovieListType.POPULAR),
        MovieFragment.newInstance(MovieListType.TOP_RATED),
        MovieFragment.newInstance(MovieListType.UPCOMING)
    )

    override fun getItem(position: Int): Fragment {
        return pages[position]
    }

    override fun getCount(): Int {
        return pages.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return when (position) {
            0 -> "Popular"
            1 -> "Top Rated"
            else -> "Up Coming"
        }
    }
}

这是我的电影片段代码

package id.cahyowhy.tmdbmovieimplementation.ui.activity.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import id.cahyowhy.tmdbmovieimplementation.databinding.MoviesFragmentResultBinding
import id.cahyowhy.tmdbmovieimplementation.ui.adapter.ItemTypeFactoryImpl
import id.cahyowhy.tmdbmovieimplementation.ui.adapter.VisitableRecyclerAdapter
import id.cahyowhy.tmdbmovieimplementation.ui.adapter.viewholder.ErrorStateItem
import id.cahyowhy.tmdbmovieimplementation.ui.adapter.viewholder.MovieItem
import id.cahyowhy.tmdbmovieimplementation.ui.base.BaseFragment
import id.cahyowhy.tmdbmovieimplementation.ui.base.BaseViewItem
import id.cahyowhy.tmdbmovieimplementation.ui.util.ext.observe
import org.koin.android.viewmodel.ext.android.viewModel

open class MovieFragment(
    private val movieListType: MovieListType
) : BaseFragment() {

    private val viewModel by viewModel<MovieFragmentViewModel>()

    private lateinit var binding: MoviesFragmentResultBinding

    private val movieAdapter by lazy {
        VisitableRecyclerAdapter(
            ItemTypeFactoryImpl(),
            ::onItemClicked
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = MoviesFragmentResultBinding.inflate(
            inflater,
            container,
            false
        )

        return binding.root
    }

    override fun onResume() {
        super.onResume()
        viewModel.loadData(0, movieListType)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        with(binding) {
            recyclerView.adapter = movieAdapter
            recyclerView.setHasFixedSize(true)

            swipeRefresh.setOnRefreshListener { viewModel.loadData(0, movieListType) }
        }

        super.onViewCreated(view, savedInstanceState)
    }

    override fun observeChange() {
        observe(viewModel.loading, ::handleLoading)
        observe(viewModel.movies, ::onDataLoaded)
        observe(viewModel.toastMessage, ::showSnackbarMessage)
    }

    private fun handleLoading(status: Boolean) {
        binding.swipeRefresh.isRefreshing = status
    }

    private fun onDataLoaded(items: List<BaseViewItem>) {
        movieAdapter.submitList(items)
    }

    private fun onItemClicked(viewItem: BaseViewItem, view: View) {
        when (viewItem) {
            is MovieItem -> {
                Log.d("MovieItem", "MovieItem Click: ${viewItem.title}")
            }
            is ErrorStateItem -> {
                viewModel.loadData(0, movieListType)
            }
        }
    }

    companion object {
        fun newInstance(movieListType: MovieListType): MovieFragment =
            MovieFragment(movieListType)
    }
}

这是我从MainActivity.kt打电话给我的时候

class MainActivity : BaseActivity() {
    private lateinit var binding: ActivityMainBinding;

    ....

    fun initView() {
        with(binding) {
            viewPager.adapter = MyPagerAdapter(supportFragmentManager)
            viewPagerTab.setupWithViewPager(viewPager)
        }
    }

谁能解决这个问题..谢谢..

评论