【问题标题】:"IllegalStateException Fragment not attached to Activity" when restarting the app重新启动应用程序时出现“IllegalStateException Fragment not attach to Activity”
【发布时间】:2014-03-20 02:06:16
【问题描述】:

我的ViewPager 中的一个Fragment 有一个奇怪的问题。我有一个ViewPager 和三个Fragment。 到目前为止一切都运行良好。当应用程序启动时,ViewPager(带有FragmentPagerAdapter)附加了三个片段,因此我可以毫无问题地从一个片段导航到另一个片段。 当我尝试手动重启应用程序(注销后)时,问题就来了:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

现在,当我注销并重新启动应用程序时,应用程序崩溃并通过 IllegalStateException Fragment not attached to Activity 尝试从片段访问资源时。

这是我附上fragments的代码,adapter

public class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public BaseListFragment getItem(int index) {

            switch (index) {
            case 0:
            case R.id.index_explore:


                BaseListFragment fragment0 = new Fragment0();
                fragment0.setUserVisibleHint(true);

                return fragment0;

            case 1:
            case R.id.index_main:

                if (fragment instanceof FragmentList)
                    ((FragmentList) fragment).removeMap();
                return new Fragment1();

            case 2:
            case R.id.index_my_stuff:

                BaseListFragment fragment2 = new Fragment2();
                fragment2.setUserVisibleHint(true);
                return fragment2;

            default:

                if (fragment instanceof FragmentList)
                    ((FragmentList) fragment).removeMap();

                return new Fragment1();

            }

        }

        @Override
        public int getCount() {
            return 3;
        }

    }

然后,我设置adpater

mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

mViewPager.setAdapter(mAppSectionsPagerAdapter);

导致崩溃的是 Fragment0。 我不明白为什么会发生这种情况,因为它应该像第一次启动应用程序时那样重新构建所有内容(因此适配器也带有片段)。 也许我认为有些事情是不正确的? 我以前搜索过一些类似的问题,并且与此异常有很多相关,但不要解决我的案例,我认为这是一个特殊的案例。

【问题讨论】:

  • 在你添加片段的地方贴出活动代码
  • 我刚刚编辑了我的问题。希望能帮助到你。谢谢。我在想问题出在重启上,因为直到现在它都能正常工作。也许我重新启动应用程序的方式不会重新创建活动,所以它不会附加片段而是使用已经分离它们的已保存实例?不知道我解释的好不好。
  • @dentex,我已经看到了,但我不知道它对我有什么帮助。还是谢谢

标签: android fragment illegalstateexception


【解决方案1】:

我想我已经解决了。正如我所料,我的问题是我“重新启动”应用程序的方式。似乎它没有再次重新创建 MainActivity,因此它没有重新构建并附加所有 fragments。 现在我restart换一种方式,我找到了here的解决方案

为此目的使用Alarmmanager 是否正确? 这是我使用的代码:

PendingIntent intent = PendingIntent.getActivity(this.getBaseContext(), 0, new Intent(getIntent()), getIntent().getFlags());
AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC, System.currentTimeMillis(), intent);
System.exit(2);

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多