设置ViewPager适配器时,应用程序崩溃

时间:2018-12-26 06:25:39

标签: java android android-fragments android-viewpager

最近,我用6种语言更新了我的应用,供用户选择。 首次打开应用程序时,系统会询问用户语言偏好。 当用户选择语言时,他/她会与主屏幕打招呼。一切正常,直到那里。

现在,用户想要更改语言。

第1步:-他/她可以进入设置并进行更改。 (这里出现问题)

设置中选择语言后,系统会要求用户重新启动该应用(手动或自动)。

应用程序自动重新启动时,该应用程序崩溃并显示以下日志-

请检查下面的Logcat

> java.lang.IllegalArgumentException: Wrong state class, expecting View
> State but received class
> android.support.design.stateful.ExtendableSavedState instead. This
> usually happens when two views of different type have the same id in
> the same hierarchy. This view's id is id/arcMenu1. Make sure other
> views do not use the same id.

完整的Logcat详细信息

> java.lang.IllegalArgumentException: Wrong state class, expecting View
> State but received class
> android.support.design.stateful.ExtendableSavedState instead. This
> usually happens when two views of different type have the same id in
> the same hierarchy. This view's id is id/arcMenu1. Make sure other
> views do not use the same id.
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455)
>         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515)
>         at android.app.ActivityThread.access$1000(ActivityThread.java:154)
>         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
>         at android.os.Handler.dispatchMessage(Handler.java:102)
>         at android.os.Looper.loop(Looper.java:157)
>         at android.app.ActivityThread.main(ActivityThread.java:5571)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
>      Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class
> android.support.design.stateful.ExtendableSavedState instead. This
> usually happens when two views of different type have the same id in
> the same hierarchy. This view's id is id/arcMenu1. Make sure other
> views do not use the same id.
>         at android.view.View.onRestoreInstanceState(View.java:14811)
>         at android.view.View.dispatchRestoreInstanceState(View.java:14786)
>         at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3124)
>         at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3130)
>         at android.view.View.restoreHierarchyState(View.java:14764)
>         at android.support.v4.app.Fragment.restoreViewState(Fragment.java:494)
>         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1486)
>         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
>         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
>         at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
>         at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
>         at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
>         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
>         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
>         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
>         at android.app.Activity.performStart(Activity.java:6374)
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2418)

所以我有这个主屏幕(主活动),带有6个选项卡(在视图寻呼机中)。此选项卡之一具有Arc Menu。日志显示arc菜单的ID已在多个地方使用。我检查了,事实并非如此。

当我在应用程序首次启动时已经设置好ViewPager adapter时,通常会发生此错误。

ViewPager

请检查ViewViewr代码的代码-

 public void setupViewPager(final ViewPager viewPager) {

        final LayoutInflater inflater = LayoutInflater.from(this);
        final Resources res = getResources();

        if (storageUtil.loadTabs() == null || storageUtil.loadTabs().isEmpty())

            adapter.addFragment(new a(), getString(R.string.a), 0);
            adapter.addFragment(new b(), getString(R.string.b), 1);
            adapter.addFragment(new c(), getString(R.string.c), 2);
            adapter.addFragment(new d(), getString(R.string.d), 3);
            adapter.addFragment(new e(), getString(R.string.e), 4);
            adapter.addFragment(new f(), getString(R.string.f), 5);

        }


        if(storageUtil.loadTabs()!=null)
        viewPager.setOffscreenPageLimit(storageUtil.loadTabs().size());
        else viewPager.setOffscreenPageLimit(6);
        viewPager.setAdapter(adapter);
        smartTabLayout.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View itemView = inflater.inflate(R.layout.custom_tab_icon, container, false);

                ImageView icon = itemView.findViewById(R.id.custom_tab_icon);
                switch (position) {

                    case 0:
                        icon.setImageDrawable(res.getDrawable(R.drawable.a));
                        break;
                    case 1:
                        icon.setImageDrawable(res.getDrawable(R.drawable.v));
                        break;
                    case 2:
                        icon.setImageDrawable(res.getDrawable(R.drawable.c));
                        break;
                    case 3:
                        icon.setImageDrawable(res.getDrawable(R.drawable.d));
                        break;
                    case 4:
                        icon.setImageDrawable(res.getDrawable(R.drawable.e));
                        break;
                    case 5:
                        icon.setImageDrawable(res.getDrawable(R.drawable.f));
                        break;
                    default:break;
                }

               return itemView;
            }
        });

        if(storageUtil.getHomeTab()==-1)
        viewPager.setCurrentItem(0);
        else viewPager.setCurrentItem(storageUtil.getHomeTab());
        setResideMenuEnabled(false);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrollStateChanged(int state) {
            }

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int x = (int) ((viewPager.getWidth() * position + positionOffsetPixels) * computeFactor());
                scrollView.scrollTo(x, 0);
            }

            public void onPageSelected(int position) {
                if (position == 0) {
                    setResideMenuEnabled(true);
                    headerTextView.animateText(getString(R.string.a));

                } else if (position == 1) {

                    headerTextView.animateText(getString(R.string.v));
                    setResideMenuEnabled(false);
                } else if (position == 2) {

                    headerTextView.animateText(getString(R.string.c));
                    setResideMenuEnabled(false);
                } else if (position == 3) {

                    headerTextView.animateText(getString(R.string.d));
                    setResideMenuEnabled(false);
                } else if (position == 4) {

                    headerTextView.animateText(getString(R.string.e));
                    setResideMenuEnabled(false);
                } else if (position == 5) {

                    headerTextView.animateText(getString(R.string.f));
                    setResideMenuEnabled(false);
                }



            private float computeFactor() {
                return (viewPagerBackground.getWidth() - viewPager.getWidth()) /
                        (float) (viewPager.getWidth() * (viewPager.getAdapter().getCount() - 1));
            }

        });

        smartTabLayout.setViewPager(viewPager);

    }
}

自从我的应用程序上一次更新以来,就一直存在此问题。我不知道如何或为什么,突然间它突然出现了(虽然更新了android studio,但它弄乱了我的java文件。Androidstudio无法将它们中的任何一个识别为java文件)。

PS-我没有在屏幕方向上做任何更改

是因为我重新启动应用程序时碎片没有被销毁而是被重新创建?

是什么原因造成的?预先谢谢你。

0 个答案:

没有答案