寻找什么违反StrictMode政策 [英] Finding what violated StrictMode policy
问题描述
我已经启用StrictMode在我的应用程序,它是造成了一些崩溃的预期。 我怎样才能找出我的code,我违反了这些政策?
I've enabled StrictMode in my app and it's causing a few crashes as expected. How can I find out where in my code I'm violating these policies?
这是堆栈跟踪:
E/AndroidRuntime(19523): FATAL EXCEPTION: main
E/AndroidRuntime(19523): android.os.StrictMode$StrictModeViolation: policy=95 violation=2
E/AndroidRuntime(19523): at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1326)
E/AndroidRuntime(19523): at android.os.StrictMode.access$1300(StrictMode.java:111)
E/AndroidRuntime(19523): at android.os.StrictMode$AndroidBlockGuardPolicy.handleViolation(StrictMode.java:1319)
E/AndroidRuntime(19523): at android.os.StrictMode$AndroidBlockGuardPolicy$1.run(StrictMode.java:1206)
E/AndroidRuntime(19523): at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(19523): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(19523): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(19523): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(19523): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19523): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(19523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
E/AndroidRuntime(19523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
E/AndroidRuntime(19523): at dalvik.system.NativeStart.main(Native Method)
但正如你所看到的......这不是很有用...我知道谁杀了我的应用程序,我需要知道为什么!
but as you can see ... it's not very useful ... I know who killed my app, I need to know why!
感谢。
推荐答案
您需要调用 penaltyLog()
您StrictMode.ThreadPolicy.Builder,这样它会显示你的根本原因,以及停止你的应用程序。
You need to call penaltyLog()
on your StrictMode.ThreadPolicy.Builder, so that it will show you the underlying reason as well as stopping your app.
下面就是你可能有目前:
Here's what you probably have currently:
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyDeath()
.build());
如果你调用主线程在网络上,你会得到这个例外,这是很难理解:
If you call the network on the main thread, you'll get this exception which is hard to understand:
E/AndroidRuntime(8752): android.os.StrictMode$StrictModeViolation: policy=71 violation=4
E/AndroidRuntime(8752): at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1311)
如果你再加入 penaltyLog()
您的保单......
If you then add penaltyLog()
to your policy...
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeath()
.build());
然后你会看到一个更加有用的信息类似下面。这将是在LogCat中输出
then you will see a much more helpful message like the one below. This will be in the LogCat output.
D/StrictMode(8810): StrictMode policy violation; ~duration=2956 ms: android.os.StrictMode$StrictModeNetworkViolation: policy=87 violation=4
D/StrictMode(8810): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1090)
如果你仔细观察,你会发现这个堆栈跟踪将带领您到code这是造成StrictMode冲突。
If you look closely, you'll see that this stack trace will lead you to the code which is causing the StrictMode violation.
这篇关于寻找什么违反StrictMode政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!