当前位置: 首页 > 知识库问答 >
问题:

PagerAdapter#NotifyDataSetChange

舒永嘉
2023-03-14

我的自定义PageAdapter类(继承自FragmentStatePagerAdapter)中有以下错误,我有10个选项卡可以找到我的产品,PageAdaptar有2个方法覆盖了公共CharSequence getPageTitle、公共int getCount()和一个构造函数。这是标记我的以下错误:->应用程序的PagerAdapter更改了适配器的内容,而没有调用PagerAdapter#NotifyDataSetChanged!估计得适配器元素数量:0,找到得数量:10分页器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blanco"
        android:scrollbars="horizontal"
        app:tabGravity="fill"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/negro"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager_menu"
        android:paddingBottom="50dp"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tablayout_perfil"/>

</LinearLayout>

紧接着就是我的自定义适配器,根据位置我返回一个片段

 public class CostumAdapter2 extends FragmentStatePagerAdapter {

        public CostumAdapter2(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();

            switch (position) {
                case 0:

                    FragmentBebidas fragmentBebidas = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas.setArguments(bundle);
                    return fragmentBebidas;

                case 1:
                    FragmentBebidas fragmentBebidas2 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas2.setArguments(bundle);
                    return fragmentBebidas2;

                case 2:
                    FragmentBebidas fragmentBebidas3 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas3.setArguments(bundle);
                    return fragmentBebidas3;


                case 3:
                    FragmentBebidas fragmentBebidas4 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas4.setArguments(bundle);
                    return fragmentBebidas4;


                case 4:
                    FragmentBebidas fragmentBebidas5 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas5.setArguments(bundle);
                    return fragmentBebidas5;


                case 5:
                    FragmentBebidas fragmentBebidas6 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas6.setArguments(bundle);
                    return fragmentBebidas6;


                case 6:
                    FragmentBebidas fragmentBebidas7 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas7.setArguments(bundle);
                    return fragmentBebidas7;


                case 7:
                    FragmentBebidas fragmentBebidas8 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas8.setArguments(bundle);
                    return fragmentBebidas8;


                case 8:
                    FragmentBebidas fragmentBebidas9 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas9.setArguments(bundle);
                    return fragmentBebidas9;
                case 9:
                    FragmentBebidas fragmentBebidas10 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas10.setArguments(bundle);
                    return fragmentBebidas10;
            }
            return null;

        }
        @Override
        public CharSequence getPageTitle(int position) {
            return menuTabs.get(position).toString();
        }
        @Override
        public int getCount() {
            return menuTabs.size();
        }


    }

在这里执行下一个操作,以膨胀包含选项卡的片段

ahBottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {
                // Do something cool here...
                switch (position) {

                    case 0:

                        //seteando el texto del menu
                        tituloActivity.setText("Menú");
                        iconBack.setVisibility(View.INVISIBLE);
                        //Toast.makeText(getApplicationContext(), "Menú", Toast.LENGTH_LONG).show();
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        MenuViewController menuViewController = new MenuViewController();
                        /**menuViewController.setArguments(bundle);**/
                        fragmentTransaction.add(R.id.myFragemnt,menuViewController);
                        fragmentTransaction.replace(R.id.myFragemnt, menuViewController);
                        fragmentTransaction.commit();


                        return true;
                    case 1:
  rootView = inflater.inflate(R.layout.menu_layout, container, false);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_menu);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager_menu);
        CostumAdapter2  costumAdapter =  new CostumAdapter2(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(costumAdapter);
        costumAdapter.notifyDataSetChanged();
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

        });

        return rootView;
    }
> 04-17 09:25:56.041: E/AndroidRuntime(30450): FATAL EXCEPTION: main
> 04-17 09:25:56.041: E/AndroidRuntime(30450): Process:
> com.blueicon.romerito, PID: 30450 04-17 09:25:56.041:
> E/AndroidRuntime(30450): java.lang.IllegalStateException: The
> application's PagerAdapter changed the adapter's contents without
> calling PagerAdapter#notifyDataSetChanged! Expected adapter item
> count: 0, found: 10 Pager id: com.blueicon.romerito:id/viewPager_menu
> Pager class: class android.support.v4.view.ViewPager Problematic
> adapter: class
> com.blueicon.romerito.controller.menuprincipal.fragmentosmenu.MenuViewController$CostumAdapter2
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.v4.view.ViewPager.populate(ViewPager.java:1167) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2178)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1155)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.selectTab(TabLayout.java:1148)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.selectTab(TabLayout.java:1118)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:916)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:900)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:813)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:774)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:752)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> com.blueicon.romerito.controller.menuprincipal.fragmentosmenu.MenuViewController$1.run(MenuViewController.java:70)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.os.Handler.handleCallback(Handler.java:739) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> android.os.Handler.dispatchMessage(Handler.java:95) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> android.os.Looper.loop(Looper.java:158) 04-17 09:25:56.041:
> E/AndroidRuntime(30450):    at
> android.app.ActivityThread.main(ActivityThread.java:7229) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> java.lang.reflect.Method.invoke(Native Method) 04-17 09:25:56.041:
> E/AndroidRuntime(30450):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

共有1个答案

上官鸿朗
2023-03-14

您将MenuViewController.costumAdapter2声明为内部类。更改MenuViewController.Menutabs的内容时,还必须在每个适配器上调用NotifyDataSetChanged()

由于在MenuViewController中甚至没有对它的引用,这可能会很困难。

尝试在MenuViewController中以单例实现适配器,并在更改Menutabs时调用NotifyDataSetChanged()

 类似资料:
  • 我有一个使用ViewPager的活动,在这个ViewPager中有一个TouchImageView(一个检测手势和相应缩放/移动的ImageView)。 现在,每当单击图像时,我想要更改UI中的一些内容,但因为TouchImageView是在PagerAdapter中声明的,所以我无法访问它,如果我要在PagerAdapter的一侧设置一些内容,我也无法访问我想要更改的UI组件(我还想在应用程序中

  • 我在StackOverflow和其他地方看到了类似的各种问题,但这些解决方案似乎都没有解决我的问题,如下所示: 我有一个带有简单PagerAdapter的ViewPager。每个“页面”都有一个与之关联的XML布局,其中至少包含一个TextView、一个ImageView,有时还包含另一个TextView。与ImageViews关联的图像在一个维度上通常为1000px,在另一个维度上更小,但它们是

  • 我正在使用幻灯片显示图像...我想将缩略图添加到它中。下面我想作为缩略图显示多少图像以及它显示的特定图像。我浏览了许多网站,但没有找到任何有用的东西

  • 我一直在使用在动态添加和删除视图到viewpager中显示的适配器,但并不是所有的都能正常工作。 我的ViewPager包含一些带有WebViews的自定义视图。我所做的是一个PagerAdapter的扩展类,它实现了与我第一篇文章的答案中所示的几乎相同的方法。 当我同时删除多个页面时,问题就来了。正如上面的答案中所解释的,对于删除页面,我们将适配器设置为空,然后再次设置。我使用的方法几乎与只使用

  • 我已经搜索了多个网站,android开发者网站,在没有运气解决我的问题后,我不得不寻求社区帮助。我们将非常感谢所有的帮助和建议。 问题: 我总共有12个从服务器接收的图像(在这里解析),我在一个PagerAdapter中显示了它们中的每一个。如果所有的值都不为空,适配器代码工作正常,问题是我允许空值存储在服务器中。当我从服务器获得整个列表时,我只想在包含not null的适配器中拥有这些视图。例如

  • 问题内容: 您好,我正在尝试在两个片段(armarFragment到cocinaFragment)之间发送数据,但是我不知道该怎么做,因为这两个片段都在同一个Activity(tabsActivity)中,后者实现了一个pagerAdaptar以显示不同片段。在这里,我把我的代码。谢谢。 tabsActivity.java(在内部可以找到类pagerAdapter)。 armarFregment.