gpt4 book ai didi

android - AsynckTask.onPostExecute 和 FragmentActivity.onResumeFragments 是否在同一线程(ui 线程)上运行?

转载 作者:行者123 更新时间:2023-11-29 00:06:14 24 4
gpt4 key购买 nike

当我遍历 ArrayList 时,我得到了 ConcurrentModificationException。我做了什么:

1.  activity has a ArrayList<Callable>
2. add Callable to ArrayList when AsyncTask.onPostExecute
3. traverse the ArrayList to call the callable in onResumeFragments

如何触发异常:我制作了 AsyncTask.execute() 并快速按下主页按钮,当我的应用程序返回前台时,异常发生了。

让我困惑的是:如果 AsyncTask.onPostExecuteFragmentActivity.onResumeFragments 在同一线程上运行,不会有 java.util.ConcurrentModificationException,对吧?

As I know AsyncTask.onPostExecute runs on ui thread, and FragmentActivity.onResumeFragments should runs on ui thread too, so how java.util.ConcurrentModificationException happend?

这是我的异常堆栈

com.yumei.sdkdemo I/PayMethodActivity: 
11-10 11:34:35.218 10030-10030/com.yumei.sdkdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yumei.sdkdemo, PID: 10030
java.lang.RuntimeException: Unable to resume activity {com.yumei.sdkdemo/com.yumei.sdk.PayMethodActivity}: java.util.ConcurrentModificationException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2954)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5235)
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:906)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)
Caused by: java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
at com.yumei.sdk.PayMethodFragmentActivity.onResumeFragments(PayMethodActivity.java:116)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:451)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:141)
at android.app.Activity.performResume(Activity.java:6040)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2943)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5235)
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:906)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)

感谢您的回复。

最佳答案

注意,如果你想在遍历时删除一些项目,你应该使用 Iterator。如果您的 AsynTask 不是在 UI 线程上创建的,请确保您使用线程安全的。

关于android - AsynckTask.onPostExecute 和 FragmentActivity.onResumeFragments 是否在同一线程(ui 线程)上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com