大家好,又见面了,我是你们的朋友全栈君。
问题
Running into some ANR issues with an android application so I implemented the StrictMode policies. Never used this before so hopefully someone can help explain the following:
Why does the log show 2 violations which are seemingly similar apart from a difference in the top 4 line and the duration? And why are there 2 violations anyway – does this mean that the code was executed twice?
Any help appreciated
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)
回答1:
The StrictMode violation is showing that you are executing an SQLite query on the UI thread which is causing the ANR issues.
Try performing database interactions on a separate thread, this will prevent the ANR issues and prevent these StrictMode violations showing.
From skimming through that LogCat output it looks like it’s occuring in conx.Activities.JobScheduleActivity.onSaveEvent
Some useful reading:
http://developer.android.com/guide/components/processes-and-threads.html
http://www.vogella.com/articles/AndroidPerformance/article.html
来源:https://stackoverflow.com/questions/11963601/android-strictmode-policy
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136754.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...