当我打开严格模式检测全部时,我的应用程序崩溃应用程序的super.onCreate()(即使我的任何代码做任何事情之前)。
我的申请onCreate打开严格模式如下
override fun onCreate() {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath().build())
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath().build())
}
super.onCreate()
// Some other code
}
我得到的错误(位于super.onCreate()
)
D/StrictMode: StrictMode policy violation; ~duration=98 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=327711 violation=2
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1263)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:182)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at android.graphics.Typeface.getFullFlipFont(Typeface.java:584)
at android.graphics.Typeface.getFontPathFlipFont(Typeface.java:532)
at android.graphics.Typeface.SetFlipFonts(Typeface.java:719)
at android.graphics.Typeface.SetAppTypeFace(Typeface.java:846)
at android.app.Application.onCreate(Application.java:110)
at com.mypackage.MyApplication.onCreate(MyApplication.kt:40)
这是我们应该忽略的预期错误,还是我们应该解决这个问题?
答案 0 :(得分:1)
显然错误是特定于设备的。它发生在三星S7上,但不是Nexus 6P。因此,这不是我要修的东西。因此,压制它的最好方法。
下面是我如何抑制它的例子。您可以将这些函数包装到Util类中。
override fun onCreate() {
turnOnStrictMode()
permitDiskReads{
super.onCreate()
}
// Some other code
}
fun turnOnStrictMode() {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath().build())
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath().build())
}
}
fun permitDiskReads(func: () -> Any) : Any {
if (BuildConfig.DEBUG) {
val oldThreadPolicy = StrictMode.getThreadPolicy()
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder(oldThreadPolicy)
.permitDiskReads().build())
val anyValue = func()
StrictMode.setThreadPolicy(oldThreadPolicy)
return anyValue
} else {
return func()
}
}
请参阅to this了解更多详情。
答案 1 :(得分:0)
将语句“ super.onCreate()”移动到onCreate()的StrictMode方法之前。查看本文以了解详细信息。 Walk through hell with Android StrictMode
答案 2 :(得分:0)
这是我的Java版本
public class StrictModeManager {
private static boolean enabled = false;
private StrictModeManager() {}
public static void enableStrictMode() {
enabled = true;
StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDropBox();
StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects()
.penaltyLog()
.penaltyDropBox()
.penaltyDeath();
StrictMode.setThreadPolicy(threadPolicyBuilder.build());
StrictMode.setVmPolicy(vmPolicyBuilder.build());
}
public static void allowDiskReads(Runnable runnable) {
StrictMode.ThreadPolicy oldThreadPolicy = null;
if (enabled) {
oldThreadPolicy = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(oldThreadPolicy).permitDiskReads().build());
}
runnable.run();
if (oldThreadPolicy != null) StrictMode.setThreadPolicy(oldThreadPolicy);
}
}
并使用它:
@Override
public void onCreate() {
if (BuildConfig.DEBUG) StrictModeManager.enableStrictMode();
StrictModeManager.allowDiskReads(App.super::onCreate);
// Code here
}