我收到了来自所有 Android 4+ 和 5+ 版本的崩溃报告,涉及我的viewpager v22 + FragmentPagerAdapter。ViewPager位于一个Fragment中,Pager适配器使用childFragmentManager。这种情况很少发生,只有大约1/200的用户会遇到。
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.void enqueueAction(java.lang.Runnable,boolean)(SourceFile:1397)
at android.support.v4.app.BackStackRecord.int commitInternal(boolean)(SourceFile:636)
at android.support.v4.app.BackStackRecord.int commitAllowingStateLoss()(SourceFile:619)
at android.support.v4.app.FragmentPagerAdapter.void finishUpdate(android.view.ViewGroup)(SourceFile:139)
at android.support.v4.view.ViewPager.void populate(int)(SourceFile:1073)
at android.support.v4.view.ViewPager.void populate()(SourceFile:919)
at android.support.v4.view.ViewPager.void onMeasure(int,int)(SourceFile:1441)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1406)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16574)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16574)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.support.v7.internal.widget.ActionBarOverlayLayout.void onMeasure(int,int)(SourceFile:453)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1406)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:16574)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
at android.view.View.measure(View.java:16574)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1955)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1151)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1333)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1038)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5890)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
根据Bugsense的面包屑,当返回到该活动时会发生此问题,尽管我无法在任何设备或内存条件下重现它,也无法使用开发人员选项中的“不保留活动”。整个应用程序仅支持纵向模式,但从横向模式的其他应用程序返回不会导致任何问题。
我正在使用onDetach技巧,在其中获取“mChildFragmentManager”并将其置为空,以及可能会使事情复杂化的fragmentPagerAdapter技巧,尽管我不认为它们对此问题有贡献。
是否有人知道如何一致地复现此崩溃或如何解决它?我不知道viewpager或片段管理器在销毁活动时如何仍然处于活动状态。 编辑:如果有帮助,这是更多代码。
public class HomePageAdapter extends FragmentPagerAdapter implements CustomPagerSlidingTabStrip.BadgeTabProvider {
public HomePageAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Parcelable saveState(){
return new Bundle();
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
//do nothing here! no call to super.restoreState(arg0, arg1);
}
@Override
public Fragment getItem(int i) {
Fragment hf;
switch (i){
case 0:
hf= MyFragment.newInstance(MyFragment.MODE_FRIENDS);
break;
case 1:
hf= OtherFragment.newInstance(parameter);
break;
default:
return null;
}
hf.setRetainInstance(false);
return hf;
}
@Override
public int getCount() {
return 2;
}
@Override
public String getPageTitle(int position) {
switch (position){
case 0:
return "Tab 1";
case 1:
return "Tab 2";
default:
return "";
}
}
}
在每个片段中,我都有
@Override
public void onDetach(){
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
Log.e("Hang frag " ,"no such field mchildfragmentmanager err " + e.toString());
//throw new RuntimeException(e);
} catch (IllegalAccessException e) {
Log.e("Hang frag " ," illegal access mchildfragmentmanager err " + e.toString());
// throw new RuntimeException(e);
}
}
编辑: 这个问题在最新版本的支持库v4 22.2中不再出现。 我还从适配器中删除了saveState和restoreState方法,但我怀疑那并不是问题所在。 这可能是一个奥秘的片段生命周期问题,我们永远不会知道答案。