将片段添加到ViewPager时,活动已被破坏

时间:2014-05-07 12:15:04

标签: android android-fragments android-viewpager fragmentstatepageradapter

我创建了一个包含动态数量片段的ViewPager,我为此FragmentStatePagerAdapter ViewPager提供了一系列片段,如下所示:

public class LightPageAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> myFragments;

    public LightPageAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
        super(fragmentManager);

        myFragments = fragments;

    }

   public Fragment getItem(int i) {

        return myFragments.get(i);

    }

   @Override
   public int getCount() {

        return myFragments.size();

    }
}

点击某些Fragment上的按钮时会获得片段列表,因此,我将以下方法添加到此Fragment,以便将此片段列表添加到FragmentStatePagerAdapter

public void fargmenting(List<Fragment> fragments){

   ViewPager view_pager = (ViewPager) MyFragment.view.findViewById(R.id.lighting_pager);
   view_pager.setAdapter(new LightPageAdapter(getChildFragmentManager(), fragments)); // this is where I'm getting the Exception

}

提示:  在我的应用中,我使用的是VerticalViewPager而不是ViewPager,但它的ViewPager具有相同的行为,但进行了一些修改不会影响整个过程。

这是日志错误

05-07 11:51:02.404: E/AndroidRuntime(3174): FATAL EXCEPTION: main
05-07 11:51:02.404: E/AndroidRuntime(3174): java.lang.IllegalStateException: Activity has been destroyed
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:161)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.standards.VerticalViewPager.populate(VerticalViewPager.java:965)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.standards.VerticalViewPager.populate(VerticalViewPager.java:811)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.standards.VerticalViewPager.setAdapter(VerticalViewPager.java:334)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.fragments.MyFragment.fargmenting(Lighting_Right.java:92)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.isolace.Lighting.assign_vertical_gangs_swipping(Lighting.java:502)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.isolace.Lighting.select_room(Lighting.java:403)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.isolace.Lighting$11.onClick(Lighting.java:351)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.view.View.performClick(View.java:4084)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.view.View$PerformClick.run(View.java:16966)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.os.Handler.handleCallback(Handler.java:615)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.os.Looper.loop(Looper.java:137)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

经过大量的努力,我终于找到了解决方案:

调用setAdapter时,Activity被销毁,因为它正在创建适配器的新Object(因为我将适配器指定为新对象):

new LightPageAdapter(getChildFragmentManager(), fragments)

那就是破坏了旧实例,包括它初始化依赖于这个实例的活动。

所以,我确实将fragmenting()方法分为两个部分,第一个部分,这是第一次调用它时为了制作第一个初始化,以及第二部分,仅更改数据且无需重新创建FragmentStatePagerAdapter的新实例:

private static LightPageAdapter lpa;
private static PagerAdapter adapter;

public void fargmenting(List<Fragment> fragments, boolean first_init){

   if(first_init){
      ViewPager view_pager = (ViewPager) MyFragment.view.findViewById(R.id.lighting_pager);
      lpa = new LightPageAdapter(getChildFragmentManager(), fragments);
   }else{
      LightPageAdapter.myFragments = fragments; // here I'm setting the fragments manually instead of passing it into a new constructor
      lpa.notifyDataSetChanged();
   }
   adapter = lpa;
   view_pager.setAdapter(adapter);

}