2012-08-15 128 views
4

在Android應用程序中遇到一些ANR問題,所以我實現了StrictMode策略。從未使用過,因此希望有人可以幫忙解釋以下內容:Android 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) 

回答

1

的StrictMode違規顯示您正在執行,這是造成的ANR問題在UI線程一個SQLite查詢。

嘗試在單獨的線程上執行數據庫交互,這將防止ANR問題並阻止顯示這些嚴格模式違規。

從通過LogCat輸出飛掠它看起來像它在conx.Activities.JobScheduleActivity.onSaveEvent

存在的一些有用的閱讀: