gpt4 book ai didi

Android StrictMode 策略

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:57 28 4
gpt4 key购买 nike

Android 应用程序遇到了一些 ANR 问题,因此我实现了 StrictMode 策略。以前从未使用过这个,所以希望有人可以帮助解释以下内容:

为什么日志显示 2 个看似相似的违规行为,除了前 4 行和持续时间不同?为什么还是有 2 次违规——这是否意味着代码被执行了两次?

感谢任何帮助

08-15 14:24:14.314: DEBUG/StrictMode(767): StrictMode policy violation; ~duration=13876 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=17 violation=1
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1791)
at com.j256.ormlite.android.AndroidCompiledStatement.execSql(AndroidCompiledStatement.java:151)
at com.j256.ormlite.android.AndroidCompiledStatement.runUpdate(AndroidCompiledStatement.java:70)
at com.j256.ormlite.stmt.StatementExecutor.update(StatementExecutor.java:382)
at com.j256.ormlite.dao.BaseDaoImpl.update(BaseDaoImpl.java:374)
at conx.Repositories.JobRepository.update(JobRepository.java:381)
at conx.Presenters.JobSchedulePresenter.onSave(JobSchedulePresenter.java:200)
at conx.Activities.JobScheduleActivity.onSaveEvent(JobScheduleActivity.java:111)
at conx.Activities.JobScheduleActivity.access$100(JobScheduleActivity.java:43)
at conx.Activities.JobScheduleActivity$2.onChildClick(JobScheduleActivity.java:169)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:588)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1877)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)

08-15 14:24:14.314: DEBUG/StrictMode(767): StrictMode policy violation; ~duration=12086 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=17 violation=1
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:52)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1809)
at com.j256.ormlite.android.AndroidCompiledStatement.execSql(AndroidCompiledStatement.java:151)
at com.j256.ormlite.android.AndroidCompiledStatement.runUpdate(AndroidCompiledStatement.java:70)
at com.j256.ormlite.stmt.StatementExecutor.update(StatementExecutor.java:382)
at com.j256.ormlite.dao.BaseDaoImpl.update(BaseDaoImpl.java:374)
at conx.Repositories.JobRepository.update(JobRepository.java:381)
at conx.Presenters.JobSchedulePresenter.onSave(JobSchedulePresenter.java:200)
at conx.Activities.JobScheduleActivity.onSaveEvent(JobScheduleActivity.java:111)
at conx.Activities.JobScheduleActivity.access$100(JobScheduleActivity.java:43)
at conx.Activities.JobScheduleActivity$2.onChildClick(JobScheduleActivity.java:169)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:588)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1877)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

StrictMode 违规表明您正在导致 ANR 问题的 UI 线程上执行 SQLite 查询。

尝试在单独的线程上执行数据库交互,这将防止出现 ANR 问题并防止显示这些 StrictMode 违规行为。

通过浏览 LogCat 输出,它看起来像是发生在 conx.Activities.JobScheduleActivity.onSaveEvent

一些有用的读物​​:

关于Android StrictMode 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963601/

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