简单的Android项目崩溃由StrictMode ThreadPolicy违规,strictmode.磁盘读取违规



我创建了一个新的Android项目,一个空的活动项目并使用了StrictMode,但由于strictmode,应用程序每次都会崩溃。磁盘读取违规

我在空活动的setContentView上提出的类似问题的答案是严格模式磁盘读取冲突,其中解决方案是即时运行,但我使用的是Android studio 3.5.1和即时运行,替换为"应用更改"。

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeath()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
}
}

这是我的日志猫,由以下原因引起:android.os.strictmode.DiskReadViolation

2019-10-08 00:05:20.821 16758-16758/com.example.myapplication D/StrictMode: StrictMode policy violation; ~duration=137 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1504)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
at java.io.File.exists(File.java:820)
at android.app.ContextImpl.getDataDir(ContextImpl.java:2375)
at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
at com.oppo.embryo.EmbryoApp.isDataAccessable(EmbryoApp.java:98)
at com.oppo.embryo.EmbryoApp.checkDecorLayout(EmbryoApp.java:122)
at com.android.internal.policy.DecorView.onResourcesLoaded(DecorView.java:2224)
at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2770)
at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2877)
at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2142)
at android.app.ActivityTransitionState.setEnterActivityOptions(ActivityTransitionState.java:160)
at android.app.Activity.performCreate(Activity.java:7323)
at android.app.Activity.performCreate(Activity.java:7306)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3118)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3289)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2012)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7199)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)
2019-10-08 00:05:20.821 16758-16758/com.example.myapplication D/AndroidRuntime: Shutting down VM
2019-10-08 00:05:20.825 16758-16758/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 16758
java.lang.RuntimeException: StrictMode ThreadPolicy violation
at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1705)
at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0(StrictMode.java:1619)
at android.os.-$$Lambda$StrictMode$AndroidBlockGuardPolicy$9nBulCQKaMajrWr41SB7f7YRT1I.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7199)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)
Caused by: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1504)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
at java.io.File.exists(File.java:820)
at android.app.ContextImpl.getDataDir(ContextImpl.java:2375)
at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
at com.oppo.embryo.EmbryoApp.isDataAccessable(EmbryoApp.java:98)
at com.oppo.embryo.EmbryoApp.checkDecorLayout(EmbryoApp.java:122)
at com.android.internal.policy.DecorView.onResourcesLoaded(DecorView.java:2224)
at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2770)
at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2877)
at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2142)
at android.app.ActivityTransitionState.setEnterActivityOptions(ActivityTransitionState.java:160)
at android.app.Activity.performCreate(Activity.java:7323)
at android.app.Activity.performCreate(Activity.java:7306)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3118)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3289)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2012)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:227) 
at android.app.ActivityThread.main(ActivityThread.java:7199) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)

崩溃是由于 .penaltyDeath((。

如果您不打算在违反严格模式时使应用程序崩溃,请将其删除。

有关详细信息,请参阅此内容。

相关内容

  • 没有找到相关文章

最新更新