ViewPager2 迁移后出现无效 ID 0x00000001 错误

问题描述

我一直在使用 ViewPager 使用 Tab 布局,但它已被弃用。 我按照官方文档迁移到ViewPager2, 一切正常,但我在每个片段上收到以下错误:

无效 ID 0x00000001 无效 ID 0x00000001 无效 ID 0x00000001

每次我在片段之间滑动时,这些都会增加.. 没有崩溃,但每次我重新打开活动时,数字都会增加到无效 ID 0x00000002 等等。

我对消除此错误的解决方案感到迷茫,但该应用程序仍然适用于新的 ViewPager2。 有什么想法可以解决错误的垃圾邮件吗?

活动代码:

//Responsible for adding the 4 tabs: NewLoot,ActiveLoot,SharedLoot,FinishedLoot
    private void setupViewPager(){
        SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager(),getLifecycle());
        adapter.addFragment(new NewLootFragment());//index 0
        adapter.addFragment(new ActiveLootFragment());//index 1
        adapter.addFragment(new FinishedLootFragment());//index 2
        ViewPager2 viewPager = (ViewPager2) findViewById(R.id.container);
        viewPager.setAdapter(adapter);
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        new TabLayoutMediator(tabLayout,viewPager,new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab,int position) {
                        //tab.setText("OBJECT " + (position + 1));
                    }
                }
        ).attach();
        tabLayout.getTabAt(0).setIcon(R.drawable.ic_search);
        tabLayout.getTabAt(1).setIcon(R.drawable.ic_arrow);
        tabLayout.getTabAt(2).setIcon(R.drawable.ic_action_name);

        tabLayout.getTabAt(0).getIcon().setColorFilter(getResources().getColor(R.color.red),PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(1).getIcon().setColorFilter(getResources().getColor(android.R.color.black),PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(2).getIcon().setColorFilter(getResources().getColor(android.R.color.black),PorterDuff.Mode.SRC_IN);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.view.getTab().getIcon().setColorFilter(getResources().getColor(R.color.red),PorterDuff.Mode.SRC_IN);
            }           @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.view.getTab().getIcon().setColorFilter(getResources().getColor(android.R.color.black),PorterDuff.Mode.SRC_IN);
            }           @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

SectionPagerAdapter 代码:

//Class that stores fragments for tabs
public class SectionsPagerAdapter extends FragmentStateAdapter {
    private static String TAG = "SectionsPagerAdapter";

    private final ArrayList<Fragment> mFragmentList = new ArrayList<>();

    public SectionsPagerAdapter(@NonNull FragmentManager fragmentManager,@NonNull Lifecycle lifecycle){
        super(fragmentManager,lifecycle);
    }

    //public ViewPagerFragmentAdapter(@NonNull FragmentManager fragmentManager,@NonNull Lifecycle lifecycle) {
    //    super(fragmentManager,lifecycle);
    //}

    public void addFragment(Fragment fragment){
        mFragmentList.add(fragment);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return mFragmentList.size();
    }
}

布局活动:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Loot.LootActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Top Section (Toolbar) -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relLayout0">

            <include
                android:id="@+id/topViewBarHelper"
                layout="@layout/snippet_top_profilebar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </RelativeLayout>

        <!-- Top Section (Toolbar) -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relLayout1"
            android:layout_below="@+id/relLayout0">

            <include layout="@layout/layout_top_tabs"/>

        </RelativeLayout>

        <!-- Middle Section (Body) -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relLayout2"
            android:layout_below="@+id/relLayout1">

            <include layout="@layout/layout_center_viewpager"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relLayout3">

            <!-- Bottom Section (Navigation) -->
            <include layout="@layout/layout_bottom_navigation_view"/>

        </RelativeLayout>

    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

布局中心viewpager:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layoutDirection="ltr">
        </androidx.viewpager2.widget.ViewPager2>

    </RelativeLayout>

</merge>

布局顶部标签:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/AppBarLayout">

            <com.google.android.material.tabs.TabLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/tab_layout"
                android:background="@drawable/white_grey_border_bottom"
                app:tabIndicatorColor="@color/red"
                app:tabIndicatorHeight="4dp">
            </com.google.android.material.tabs.TabLayout>

        </com.google.android.material.appbar.AppBarLayout>

    </RelativeLayout>

</merge>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)