java.lang.IllegalStateException:活动已在Fragment上销毁

时间:2016-03-16 12:22:37

标签: java android android-fragments illegalstateexception

我有一个带ViewPager的customFragment。视图寻呼机中有2个片段。索引1和索引2的片段。

public class ControllerFragment extends Fragment  {

public ControllerFragment() {

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.frame_controller, container, false);

    viewPager = (ViewPager) rootView.findViewById(R.id.demo_pager);
    viewPager.setAdapter(new pagerAdapter(getChildFragmentManager()));
    viewPager.setCurrentItem(1);
    viewPager.setPageMargin(-150);


    return rootView;


}

// viewpager adapter

public class pagerAdapter extends FragmentStatePagerAdapter {


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


    @Override
    public Fragment getItem(int position) {
        if (position == 0) {

            return indexOneRoot.get();
        }
        return new indexTwoRootFragment().newInstance(ControllerFragment.this);
    }

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

我将此片段作为另一个类中的自定义视图加载。

// xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
class="com.ide.ControllerFragment"
android:id="@+id/controllerFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

// java class

public class Test extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_screen);
    ControllerFragment controllerFragment = (ControllerFragment) getSupportFragmentManager().findFragmentById(R.id.controllerFrame);
    controllerFragment.loadIndexOne(new DemoFragment());// in this line, i want to change fragment at indexOne of the customFragment
}

// loadIndexOneFragment

public void loadIndexOne(Fragment fragment) {   indexOneRoot.get().getChildFragmentManager().beginTransaction().replace(R.id.indexOneFragment, fragment).commitAllowingStateLoss();
    }

// indexOneRoot基本上是声明为:

的customFragment中的弱引用
private WeakReference<IndexOneRootFragment> indexOneRoot = listDrawerRoot = new WeakReference<>(new IndexOneRootFragment());

当controllerFragment.loadIndexOne(new DemoFragment());被叫,它抛出

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ide.sliderdemo/com.ide.Test}: java.lang.IllegalStateException: Activity has been destroyed
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:145)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5319)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
                                                               Caused by: java.lang.IllegalStateException: Activity has been destroyed
                                                                  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
                                                                  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
                                                                  at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:617)
                                                                  at com.ide.sliderdemo.ControllerFragment.loadListDrawer(ControllerFragment.java:97)
                                                                  at com.ide.sliderdemo.Sample.LibraryTest.onCreate(Test.java:23)
                                                                  at android.app.Activity.performCreate(Activity.java:5976)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404) 
                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:145) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5319) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 

我已经检查了其他线程以寻找解决问题的方法,但没有任何工作。我错了什么感谢

0 个答案:

没有答案