查找违反StrictMode策略的内容

时间:2012-03-20 03:19:09

标签: android android-strictmode

我已经在我的应用中启用了StrictMode,并且正如预期的那样导致一些崩溃。 如何找到我的代码中的哪些内容违反了这些政策?

这是堆栈跟踪:

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)

但正如你所看到的......它不是很有用......我知道谁杀了我的应用程序,我需要知道原因!

感谢。

3 个答案:

答案 0 :(得分:19)

您需要在StrictMode.ThreadPolicy.Builder上调用penaltyLog(),以便它显示基本原因并停止您的应用。

以下是您目前可能拥有的内容:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyDeath()
.build());

如果你在主线程上调用网络,你将得到这个很难理解的异常:

E/AndroidRuntime(8752): android.os.StrictMode$StrictModeViolation: policy=71 violation=4
E/AndroidRuntime(8752):     at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1311)

如果您随后将penaltyLog()添加到您的政策中......

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeath()
.build());

然后你会看到一个更有用的信息,如下图所示。这将在LogCat输出中。

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)

如果你仔细观察,你会发现这个堆栈跟踪会引导你找到导致StrictMode违规的代码。

答案 1 :(得分:1)

每当我看到像这样的堆栈跟踪时,我总是会查看我的Activity生命周期事件。查看onCreate,onResume,onPause方法中发生了什么(有更多生命周期事件,但这些是常见事件)。在这些方法中放置断点,看看哪一个终止于这个致命的消息。然后从那里拿走它。

尝试使用

捕获此错误
protected void onResume() {
  super.onResume();
  try {
    codeThatCrashesBecauseOfStrictMode();
  } catch(Throwable tr) { Log.e(tr); }
}

这应该是调试此问题的一个很好的起点。

答案 2 :(得分:1)

StrictMode(android.os.StrictMode)类可用于启用和实施可以检查和报告的各种策略。

执行在主UI线程上执行磁盘写入时发生的磁盘写入冲突时,这可能是StrictMode违规。要解决此问题,您需要将磁盘写入主线程。

如果此时无法移动代码,则可以禁用对部分代码的检查。

明确添加代码以在执行违规代码之前停止检查特定规则违规,然后在违规代码完成后重新启用对该规则的检测。

StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
    .permitDiskWrites()
    .build());
doCorrectStuffThatWritesToDisk();
StrictMode.setThreadPolicy(old);

源自here.

的源代码