如何防止片段(例如Alert Dialogfrom)在设备语言更改或恢复状态下被销毁?

How to prevent a fragment (e.g. Alert Dialogfrom) from being destroyed in device languege change or in resume state?

提问人: 提问时间:10/14/2023 更新时间:10/14/2023 访问量:23

问:

当我更改设备语言时,我的应用程序崩溃了,其中有两部分错误。类似的帖子: 当用户更改设备语言时,应用程序崩溃,当我的手机语言设置为波斯语或阿拉伯语时,我的应用程序崩溃,因为 Double.parseDoublelogcat

android.view.WindowLeaked: Activity com.myapp.MainActivity has leaked window DecorView@f97fb0a[MainActivity] 
that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:733)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:717)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:399)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
at android.app.Dialog.show(Dialog.java:340)
at com.myapp.dialog.showDialog(DialogCenter.java:190)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3147)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:588)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Shutting down VM
com.myappFATAL EXCEPTION: main
com.myappProcess: com.myapp, PID: 8843
com.myappjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainActivity}: android.view.InflateException: Binary XML file line #59 in com.myapp:layout/main_activity: Binary XML file line #59 in com.myapp:layout/main_activity: Error inflating class fragment
com.myappat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
com.myappat android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
com.myappat android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5456)
com.myappat android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5362)
com.myappat android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
com.myappat android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
com.myappat android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
com.myappat android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
com.myappat android.os.Handler.dispatchMessage(Handler.java:106)
com.myappat android.os.Looper.loop(Looper.java:223)
com.myappat android.app.ActivityThread.main(ActivityThread.java:7656)
com.myappat java.lang.reflect.Method.invoke(Native Method)
com.myappat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
com.myappat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
com.myappCaused by: android.view.InflateException: Binary XML file line #59 in com.myapp:layout/main_activity: Binary XML file line #59 in com.myapp:layout/main_activity: Error inflating class fragment
com.myappCaused by: android.view.InflateException: Binary XML file line #59 in com.myapp:layout/main_activity: Error inflating class fragment
com.myappCaused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.floatingactionbutton.FloatingActionButton.setBackgroundTintList(android.content.res.ColorStateList)' on a null object reference
com.myappat com.myapp.LocationFragment.onViewCreated(LocationSelectionFragment.java:143)
com.myappat androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3147)
com.myappat androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:588)
com.myappat androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
com.myappat androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:114)
com.myappat androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1455)
com.myappat androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3034)
com.myappat androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2945)
com.myappat androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3148)
com.myappat androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:410)
com.myappat androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:271)
com.myappat androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:142)
com.myappat androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:136)
com.myappat androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:247)
com.myappat androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:226)
com.myappat android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
com.myappat android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
com.myappat android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
com.myappat android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
com.myappat android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
com.myappat android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
com.myappat android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
com.myappat android.view.LayoutInflater.inflate(LayoutInflater.java:680)
com.myappat android.view.LayoutInflater.inflate(LayoutInflater.java:532)
com.myappat android.view.LayoutInflater.inflate(LayoutInflater.java:479)
com.myappat androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:775)
com.myappat androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197)
com.myappat com.myapp.MainActivity.onCreate(MainActivity.java:115)
com.myappat android.app.Activity.performCreate(Activity.java:7994)
com.myappat android.app.Activity.performCreate(Activity.java:7978)
com.myappat android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
com.myappat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
com.myappat android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
com.myappat android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5456)
com.myappat android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5362)
com.myappat android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
com.myappat android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
com.myappat android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
com.myappat android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
com.myappat android.os.Handler.dispatchMessage(Handler.java:106)
com.myappat android.os.Looper.loop(Looper.java:223)
com.myappat android.app.ActivityThread.main(ActivityThread.java:7656)
com.myappat java.lang.reflect.Method.invoke(Native Method)
com.myappat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
com.myappat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
AppsFilter              system_process                       I  interaction: PackageSetting{46efb40 com.myapp/10154} -> PackageSetting{74eaa79 com.android.vending/10099} BLOCKED
ActivityTaskManager     system_process                       W  Force finishing activity com.myapp/.MainActivity
Process                 com.myapp                            I  Sending signal.
WIN DEATH: Window{bff4098 u0 com.myapp/com.myapp.MainActivity}

我已经在另外两个应用程序中尝试了这个测试。

  • 在电报应用程序中,当我打开包含警报对话框的页面时,我暂停应用程序然后更改设备语言,但返回应用程序时会关闭警报对话框。Telegram 在任何暂停状态下关闭任何弹出的对话框。

  • 我在另一个应用程序中测试了此行为。在该应用程序中,对话框以简单的暂停状态保持打开状态,但是当我暂停应用程序并更改设备语言并返回应用程序时,对话框会消失。

  • 我为“Android 权限对话框”测试了此行为。令人惊讶的是,它仍然处于打开状态,没有任何应用程序崩溃或错误。

问题:如何解决这个问题?

我想我应该在方法上做点什么。(请注意,我在片段中有许多警报对话框。onPause()onResume()

能否实现“Android权限对话框”的健壮行为?

我认为由于上述问题,一个候选人如下:

主要问题:在我的所有片段中,恢复模式都会触发片段中的某些操作。重新打开警报对话框,触发滑动刷新布局,按下一些按钮等。为什么会发生这种情况以及如何防止这种不良行为?

android android-fragments 错误处理 android-alertdialog

评论


答: 暂无答案