应用程序崩溃后更改手机语言并从后台

时间:2016-07-15 05:23:06

标签: android localization multilingual android-anr-dialog

我正在开发一个Android应用程序并面临一些奇怪的问题。当应用程序在后台和用户更改手机语言并从后台重新启动应用程序然后应用程序停止。

奇怪的是,只有当我将语言从英语更改为法语时,应用程序才会崩溃,但是当从法语更改为英语或其他语言时,它会在没有任何崩溃的情况下发挥作用。以下是错误日志 -

  

流程:com.myapp,PID:17745                                                                java.lang.IllegalStateException:活动已被销毁                                                                    在android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)                                                                    在android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)                                                                    在android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)                                                                    在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:162)                                                                    在android.support.v4.view.ViewPager.populate(ViewPager.java:1177)                                                                    在android.support.v4.view.ViewPager.populate(ViewPager.java:1025)                                                                    在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:715)                                                                    在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:715)                                                                    在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                                    在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)                                                                    在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)                                                                    在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                                    在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)                                                                    在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                                    在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)                                                                    在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)                                                                    在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                                    在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                                    在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)                                                                    在android.widget.LinearLayout.measureVertical(LinearLayout.java:748)                                                                    在android.widget.LinearLayout.onMeasure(LinearLayout.java:630)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)                                                                    在android.widget.FrameLayout.onMeasure(FrameLayout.java:194)                                                                    在com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643)                                                                    在android.view.View.measure(View.java:18794)                                                                    在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)                                                                    在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)                                                                    在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)                                                                    在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)                                                                    在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)                                                                    在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)                                                                    在android.view.Choreographer.doCallbacks(Choreographer.java:670)                                                                    在android.view.Choreographer.doFrame(Choreographer.java:606)                                                                    在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                    在android.os.Looper.loop(Looper.java:148)                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:0)

这似乎是从为嵌套片段添加的支持功能中出现的 bug

基本上,当FragmentManager与活动分离时,内部状态会中断。

在片段上试试这个:

@Override
public void onDetach() {
    super.onDetach();
    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

让我知道它是否有效。

此致

答案 1 :(得分:0)

这是因为当您更改设备的语言时,会创建片段的新实例,并且应用程序会尝试查找新实例。

你需要存储你的碎片' List中的实例以避免此类型异常。

如果您提供添加片段的代码,那么它将更加清晰。